while(<$fh>) reads the file line by line, so even with /sm the string that is tested for a match is either the line that contains the printf, or the line that contains the end ';' (if it spans over multiple lines). You can read the documentation on the input record separator: $/ which defines how much perl reads from a file on each iteration. By default the value of $/ is "\n" which will make perl stop everytime it sees an end of line. But you can also set it to undef to read (slurp) the whole file at once. Slurping will give wrong results though as the string:

printf(""); sleep(); print("");
will match only once because .* (with /s) will read the whole thing up to the last ;. Even line by line "printf(""); "printf(""); will only match once. One way to avoid that is to replace .* by .*? which means the shortest match possible (each time perl matches a new character it will try to stop the regex by looking for a ; before "eating" anything else with the dot)

In the best case scenario you can do something like:

{ local $/ = ";"; # local means perl will restore the value of $/ at t +he end of the block # and the default behaviour when reading a file for my $file (@files) { # Note the $!, which contains the reason for the opening failure open my $fh, '<', $file or warn "Coudn't open $file: $!\n"; while (<$fh>) # will stop at each ";", so read instruction by ins +truction { push @printfs, $1 if /(printf.*);/; } } }

Note that a case like printf(";"); will make your regex yield an unexpected result either with .*? or $/ = ";". This comes from the fact that C code is actually not a regular language, which means it can't be parsed by regular expressions. Luckily, perl regexes are far more powerful than simple regular expressions, even if the name seems to say otherwise. Making a foolproof C parser is still a really difficult (and kind of foolish) thing to do with regexes. So if you're quite sure of your input data and know it fits a certain pattern, you're probably safe with regexes, but if you notice you keep getting harder and harder to include exceptions, give up that path (what you would need then is a grammar parser).


In reply to Re: Catching multiple lines and assigning them as a single array element by Eily
in thread Catching multiple lines and assigning them as a single array element by kort

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.