One way to slurp

From JmPm

(Difference between revisions)
Jump to: navigation, search
m (Protected "One way to slurp": spam [edit=autoconfirmed:move=autoconfirmed])
m (Unprotected "One way to slurp": global protection)
 

Current revision as of 17:34, 18 March 2007

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