Adding subroutines like the one limal suggests would help isolate the problem. You could test each subroutine independently on a relatively small subset of files, so if it's slow, you can at least isolate it to a certain chunk of code. Right now, you have too much to look at at once. I look to keep my subroutines to a screenful (about 35 lines for me).

Avoiding global variables, i.e. declaring a variable within a subroutine with my, also helps keep the problem isolated. You only pass in and out of the subroutine what you have to. For instance, you could move your while statement to a subroutine, like

while (<PATTERN>){ # do stuff here }
You could pass in the filehandle <PATTERN> saved as a variable, and make it more readable. To do that, you would replace your current statement:

open (PATTERN, "<", "$html_dir/$FigFile/$DirFile") or die "Unable to +open pattern file: $1";
with:
open my $PATTERN, "<", "$html_dir/$FigFile/$DirFile" or die "Unable to open pattern file: $!";
and later, pass the open filehandle into a subroutine, like:
DoSomethingWithFile($PATTERN);
where
sub DoSomethingWithFile { my $FH = shift @_; <code> while (<PATTERN>){ # do stuff here } }
(You might notice that in my open statement above, I replaced $1 with $!, which is I think what you intended. $1 is the variable used to store the first match in a regular expression, and the value returned if a system or library call fails. As it says in perlvar, a mnemonic is 'What just went bang?'.

One thing that would really help with the formatting is if the beginning and ending of each curly brace were lined up. For instance, the fact that you have at the end of your code two curly braces in the same horizontal position makes it harder to read and figure out the flow of the program.

-- Burvil


In reply to Re: Exceeding CPU Limit by bowei_99
in thread Exceeding CPU Limit by xdbd063

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.