Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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> };

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
Node Status?
node history
Node Type: note [id://662140]
help
Chatterbox?
[usemodperl]: stop asking why, just say how
usemodperl pulls teeth...
[usemodperl]: change the world? you mean BREAK THE WEB? we will regret this once google starts pulling certs for cursing or something...
Veltro Sees open mouth and feeds troll
[Corion]: usemodperl: Naah, you're just being unreasonable. It's not hard to download https URLs, and we've shown you ways to avoid downloading at all, and ways to keep downloading via http possible. You reject both.
[choroba]: Also, try asking at IRC
[usemodperl]: corion baby you have done nothing of the sort
[usemodperl]: asking how to download from cpan with core perl via http is unreasonable???
[Corion]: usemodperl: No, asking is not unreasonable. Insisting that it must always keep working for you is unreasonable.
[Corion]: But again, you haven't explained why your users need to download, or why they need to download via http, or why you can't host the SSL-stripper yourself.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-06-24 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?