http://www.perlmonks.org?node_id=662140


in reply to $/ question

If you're undef'ing it, then you're in slurp mode, and there's no point in looping, because there's only going to be one line in the file ...
sub slurpie { local $/ ; return <DATA>; # contains whole thing } sub DATA_w_line_numbers { while (<DATA>) { print "$.) $_"; } }
Also be sure to read through all off the perlvar section on $/ -- i think you're looking for local $/ = "";

This may be of interest as well: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

Replies are listed 'Best First'.
Re^2: $/ question
by convenientstore (Pilgrim) on Jan 13, 2008 at 03:13 UTC
    thanks, checking it out now
      Below example do not work either.. after putting into paragraph mode
      why?
      I get --> sdfsdfsdf23423
      #!/usr/bin/perl -w
      
      use strict;
      
      sub slurpie {
            while (<DATA>) {
                 local $/ = "";   # put into paragraph mode (separated by one or more blank lines
                 next unless /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg;  #should only match first paragraph
                 print "$_\n";
            }
      }
      
      
      slurpie();
      
      __DATA__
      hi
      hi
      234
      hi
      
      hoi
      sdfsdfsdf23423
      hi
      
      hi
      hi
      1234
      
      1
      

        You're setting a local value to $/ only inside the read loop. That is, the value gets set after the first record is read. At the end of the loop, it's set back to its default value ("\n"), and the next record is read with that value. Then inside the loop, you set the local value again. Try it this way:

        sub slurpie { local $/ = ''; # paragraph mode while (<DATA>) { next unless /^\w\w.+(\w\w).+(\d+).+(\w\w)/sg; #should only ma +tch first paragraph print "$_\n"; } }