One way to slurp

From JmPm

Revision as of 17:34, 18 March 2007 by Amire80 (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Slurp means to read a whole file into a variable:

  open( my $fh, $filename ) or die "cannot open file $filename $!\n";
  my $text = do { local( $/ ) ; <$fh> } ;

this snippet of code shows how you can locally undef the variable $/ which contains the newline character. This pulls the whole file into $text and then automatically goes back to its original value when out of the block.

Personal tools