Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: $/ question

by davidrw (Prior)
on Jan 13, 2008 at 02:56 UTC ( #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> };


Comment on Re: $/ question
Select or Download Code
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
Node Status?
node history
Node Type: note [id://662140]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-08-29 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls