Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: $/ question

by davidrw (Prior)
on Jan 13, 2008 at 02:56 UTC ( [id://662140]=note: print w/replies, xml ) Need Help??


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"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found