Another trick to reduce processing time is to compose all the  @strings_to_be_matched strings into a single regex.

c:\@Work\Perl\monks>perl -wMstrict -le "my @strings_to_be_matched = qw(foo bar wibble wobble fee_fie foe fum) +; ;; my ($ur) = map qr{ $_ }xms, join q{ | }, map quotemeta, reverse sort @strings_to_be_matched ; print qq{\$ur: $ur}; ;; my $reg3 = qr/extern.+\b$ur\b\s*/i; print qq{\$reg3: $reg3}; " $ur: (?^msx: wobble | wibble | fum | foo | foe | fee_fie | bar ) $reg3: (?^i:extern.+\b(?^msx: wobble | wibble | fum | foo | foe | fee_ +fie | bar )\b\s*)
I'm making a couple of assumptions: If these assumptions are true, then a couple of steps in creating the  $ur regex are redundant, but will do no harm.

So your final code might look something like this (untested):

my @strings_to_be_matched = ...; my ($ur) = map qr{ $_ }xms, join q{ | }, map quotemeta, reverse sort @strings_to_be_matched ; my $reg1 = qr/=/i; my $reg2 = qr/\S+=\S+/i; my $reg3 = qr/extern.+\b$ur\b\s*/i; my $reg4 = qr/;$/i; my $reg5 = qr/.+\b$ur\b\s*/i; foreach my $ln (@contents_of_file) { if ($ln =~ $reg3 and $ln =~ $reg4) { ... } if ($ln =~ $reg5 and $ln=~ $reg4 and ($ln !~ $reg1 or $ln =~ $reg2 +)) { ... } if ($ln =~ $reg3 and $ln !~ $reg4) { ... } if ($ln =~ $reg5 and $ln !~ $reg4 and $ln !~ $reg1) { ... } }
(But please see Discipulus's remarks above about using a while-loop rather than a for-loop for processing file contents line-by-line.)

Another thing that may affect speed is that you have all your regexes  $reg1 $reg2 $reg3 $reg4 $reg5 modified as  /i (case insensitive). Case insensitivity slows down regex execution. Some of your regexes have only assertions, characters or character classes  \S $ ; = to which case insensitivity does not apply. As noted above, the  @strings_to_be_matched strings seem to be C/C++ or suchlike keywords or identifiers; is case insensitivity ever appropriate here? I would seriously reconsider the use of case-insensitivity.

Last but not least: As a beginner, it's important always to usewarnings; and usestrict; and avoid global variables.


Give a man a fish:  <%-{-{-{-<


In reply to Re^7: Using regex with a variable by AnomalousMonk
in thread Using regex with a variable by Praveen Dharmaraj

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.