Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

$/ question

by convenientstore (Pilgrim)
on Jan 13, 2008 at 02:29 UTC ( [id://662137]=perlquestion: print w/replies, xml ) Need Help??

convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

I thought setting $/ to undef would make record separated by one blank line or more.. and resulted in each record w/ separate line number

Instead I am getting number 1 and then entire information being printed.

Did I misunderstand the $/ ? or am I looping it wrong?

I was expecting a result like this

--expected results-- 1hi hi hi 2hi hi 3hi hi

------------------------------------------

[root@myserver tmp]# cat perl_slurp.pl #!/usr/bin/perl -w use strict; sub slurpie { local $/ ; while (<DATA>) { print "$. $_\n"; } } slurpie(); __DATA__ hi hi hi hi hi hi hi [root@myserver tmp]# ./!$ ./perl_slurp.pl 1 hi hi hi hi hi hi hi ___________________________________________________________

*** UPDATE***

I am confused.. how does differ using $/ in inside of while loop vs outside.. local is immediate block so it should be inside of while loop?

#!/usr/bin/perl -w use strict; sub slurpie { while (<DATA>) { local $/ ; next unless /^\d+/; print "$. $_"; } } slurpie(); __DATA__ hi hi 234 hi hoi sdfsdfsdf23423 hi hi hi 1234 1 [root@myserver tmp]# ./!$ ./././././././././perl_slurp.pl 3 234 12 1234 14 1

Replies are listed 'Best First'.
Re: $/ question
by davidrw (Prior) on Jan 13, 2008 at 02:56 UTC
    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> };
      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
        

Log In?
Username:
Password:

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

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

    No recent polls found