Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: getfile( $filename )

by harleypig (Scribe)
on Jul 29, 2005 at 03:46 UTC ( #479229=note: print w/ replies, xml ) Need Help??


in reply to Re: getfile( $filename )
in thread getfile( $filename )

Yeah, I've run across that little ditty before, most recently as a yak shaving on my IPC::Open3 journey, but it doesn't handle wantarray. It just occurred to me that it doesn't chomp the lines either ... hmmm ...

return wantarray ? do { my @lines = <$FH> ; chomp @lines ; @lines } : do { local $/ ; <$FH> };
I wish there was a way to chomp <$FH>.
Harley J Pig


Comment on Re^2: getfile( $filename )
Download Code
Re^3: getfile( $filename )
by merlyn (Sage) on Jul 29, 2005 at 03:55 UTC
      Thanks! Updated.
      Harley J Pig
      Why not my @result = map scalar(chomp, $_), <$FH>?

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        First, I wouldn't have expected scalar to take two arguments. True, modern perlfunc pages document this, but it's certainly unexpected to me.

        Second, I dislike modifying $_ in a map, because I believe a map to be applicative, not side-effect-ish.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        I'm not sure how that works, but it seems like the map /(.*)/, <> is more perlish. Also, it's shorter.
        Harley J Pig

        Why not my @result = map scalar(chomp, $_), <$FH>?

        That use of scalar gives me the creeps! Isn't this equivalent and less obfuscated?

        my @result = map {chomp; $_} <$FH>;

        Update: or for that matter whats wrong with chomp(my @result = <$FH>);

        --
        Murray Barton
        Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-08-20 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (118 votes), past polls