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

Re: Re^3: Cheap idioms

by Sihal (Pilgrim)
on Oct 14, 2002 at 18:46 UTC ( [id://205173]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Cheap idioms
in thread Cheap idioms

Im sorry but I fail to understand how both these idioms work. I can see why it slurps a file, but cant really say exactly how... Could somebody explain the magic with ARGV?

Replies are listed 'Best First'.
Re^5: Cheap idioms
by Aristotle (Chancellor) on Oct 15, 2002 at 09:34 UTC

    The diamond operator (<>) will go through all elements of @ARGV in sequence, open and read them, one after another. So you localize @ARGV, meaning you make a "private copy" of it for the duration of the block, put just one filename in it, the one you wish to read, and also make a local copy of $/ (which contains what Perl is to assume an end-of-line; see perldoc perlvar) without actually putting any value in it, meaning you'll slurp the entire content from any file handle at once. As the diamond operator is the last thing in the block, its result becomes the "return value" of the block.

    And thus, the slurped file content ends up in the variable.

    Makeshifts last the longest.

      Okay thanks for the explanation. I didn't know that <>when used with argv would automatically open the file. Nice trick.

Log In?
Username:
Password:

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

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

    No recent polls found