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

given and for

by GlitchMr (Sexton)
on Aug 21, 2012 at 08:54 UTC ( #988652=perlquestion: print w/replies, xml ) Need Help??
GlitchMr has asked for the wisdom of the Perl Monks concerning the following question:

After you will type at least use 5.010 you can use given statement. It allows you to use when inside it, but for/foreach also allows it. I would like to know why Perl has given when for can be used for this same thing (at least I think so).

For background, in this example, both code samples are equivalent.

for ($value) { when (1) { say "One!" } when (2) { say "Two!" } default { say "Something!" } }
given ($value) { when (1) { say "One!" } when (2) { say "Two!" } default { say "Something!" } }

Replies are listed 'Best First'.
Re: given and for
by moritz (Cardinal) on Aug 21, 2012 at 09:02 UTC

    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.

      "Switch Statements

      Starting from Perl 5.10.1 (well, 5.10.0, but it didn't work right), you can say [...]"
Re: given and for
by Arunbear (Parson) on Aug 21, 2012 at 09:41 UTC
Re: given and for
by MidLifeXis (Monsignor) on Aug 21, 2012 at 12:58 UTC

    Why does Perl have while when we have if / goto? They can both do the same thing, right?

    Just because you can shoehorn some construct into doing something else does not mean that that it is the best tool for the job. It may not be the clearest indication of the programmers intent, may cause confusion with a person later reading the code, and so on. To me, there would be a disconnect between the for / when, and every time I would read it, there would be a short time where I would have to do a mental switch between "this is a loop" and "this is a set of choices based on a value". For the given / when, I would immediately be expecting a set of choices.

    There is also a false assertion in your question - that the two constructs do the same thing. See the output for perl -MO=Terse ... for the two samples given. moritz has gone into some of the differences between them in his post.


Re: given and for
by 2teez (Priest) on Aug 21, 2012 at 10:13 UTC

    given and when are analogous to switch and case in other languages. Please, see subheading " Switch Statements " under documentation perlsyn for more details.

      I know that they are analogous, but that wasn't what I asked. I asked why Perl has given when for can also be used for this.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988652]
Approved by Arunbear
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-03-29 06:38 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (344 votes). Check out past polls.