Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Re: given and for

by moritz (Cardinal)
on Aug 21, 2012 at 09:02 UTC ( #988657=note: print w/ replies, xml ) Need Help??


in reply to given and for

First of all for and given are not equivalent, they differ subtly in the scoping of their $_ variable:

use strict; use warnings; use 5.010; $_ = 42; sub f { say } given (23) { f } for (23) { f } __END__ 42 23

So given creates a new lexical $_, while for localizes the existing $_, which results in dynamic scoping.

But the real reason is that code must be readable to humans, and for has the connotation of a loop, whereas given makes it implicit that it's one value that is talked about. So it's all about intent.


Comment on Re: given and for
Select or Download Code
Re^2: given and for
by Monk::Thomas (Sexton) on Aug 24, 2012 at 09:20 UTC
    "Switch Statements

    Starting from Perl 5.10.1 (well, 5.10.0, but it didn't work right), you can say [...]"
    -- http://search.cpan.org/~rjbs/perl-5.16.1/pod/perlsyn.pod#Switch_Statements

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-04-16 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls