Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Match pattern per line and after another one as multiline in single file opening

by kennethk (Abbot)
on Feb 14, 2017 at 23:31 UTC ( [id://1182030]=note: print w/replies, xml ) Need Help??


in reply to Re: Match pattern per line and after another one as multiline in single file opening
in thread Match pattern per line and after another one as multiline in single file opening

TIMTOWDI, but I would suggest you replace:
my $data; open my $FH, '<' , $config or die "Cannot open $config: $!\n"; local $/ = undef; s/^\s*#.*\n|^\n//gm for $data = <$FH>;
with
open my $FH, '<' , $config or die "Cannot open $config: $!\n"; local $/ = undef; my $data = <$FH>; $data =~ s/^\s*#.*\n|^\n//gm;
The use of the for loop on a value you expect to be scalar confuses casual perusal, and you've declared the variable (my) at a different spot than where you initialize it. If you want to go compound, there's always
(my $data = <$FH>) =~ s/^\s*#.*\n|^\n//gm;
but that feels crowded to me. If I were actually writing this, I would do:
my $data = do { local $/ = undef; open my $FH, '<' , $config or die "Cannot open $config: $!\n"; <$FH>; }; $data =~ s/^\s*#.*\n|^\n//gm;
Slurping in a do loop keeps the filehandle tightly scoped and keeps that localization of the input file separator actually local. And note, even then, I personally prefer keeping the processing separate from the import.

Update: Fixed typos; haukex++

Update: Fixed typos; haukex++. Some days you just shouldn't post code.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

  • Comment on Re^2: Match pattern per line and after another one as multiline in single file opening
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1182030]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-25 01:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found