[Phoenix-pm] given/when in Perl 5

Scott Walters scott at illogics.org
Mon Mar 13 08:10:48 PST 2006


Hey Bobby,

Yeah, this was the subject of some debate.  Larry felt that given/when
are adequately different from switch/case that they should have different
names to avoid confusion.  And that's the flip-side of the newbie coin --
a large percentage of novice questions in the IRC help channels stem from
expecting one thing to work exactly like another, and novices are generally
extremely persistant in these expectations... they hold on too strongly
to the familiar and don't let go.

As to why it's different, all of the "smart match" semantics apply in
when(), such as array intersection tests.    

And of course this leaves the door open to have a case/switch that 
computes tables, just like C's does, and offers a speed advantage for
purely numeric comparisons.

So many opinions about Perl 6...

-scott



On  0, "Metz, Bobby W, WCS" <bwmetz at att.com> wrote:
> Aside from the fact that given/when is easier to read, why those names
> instead of case?  If I were a "case language" guy moving to Perl and
> looking for a case equivalent, I wouldn't think to search for those
> words in my handy-dandy Perl book index.  I'll just assume it has a good
> cross-index I suppose.
> 
> B
> 
> -----Original Message-----
> From: phoenix-pm-bounces+bwmetz=att.com at pm.org
> [mailto:phoenix-pm-bounces+bwmetz=att.com at pm.org]On Behalf Of Scott
> Walters
> Sent: Sunday, March 12, 2006 10:59 PM
> To: Michael Friedman
> Cc: phoenix-pm at pm.org
> Subject: Re: [Phoenix-pm] given/when in Perl 5
> 
> 
> Hey Michael,
> 
> Perl "doesn't need" a lot of things... named parameters, strong type
> checking, etc... of course, you don't *need* anything more than a 
> some toggle switches to toggle in machine code... but certain things
> at certain times are nice to have.  In this case, switch (no pun
> intended) has been in the todo since the early days (reportedly),
> but there were too many scenarios to consider so it never got done.
> Damian Conway made a pass at the problem with Switch.pm, this got
> adopted in a modified form into Perl 6's design documents, and then
> that official-making-ness was enough to get it cleared for Perl 5.
> Funny how these things work...
> 
> Cheers!  Good to see you around on the list by the way =)
> 
> -scott
> 
> On  0, Michael Friedman <friedman at highwire.stanford.edu> wrote:
> > I thought "Perl doesn't need a case statement"?
> > Or is this Perl 6 getting implemented in Perl 5 just because they
> can...
> > 
> > -- Mike
> > 
> > On Mar 12, 2006, at 12:36 AM, Scott Walters wrote:
> > 
> > > Hi happy Perl people,
> > >
> > > Here's a cut and paste from the perldoc perlsyn from 5.9.3:
> > >
> > >            use feature ":5.10";
> > >            given($foo) {
> > >                when (undef) {
> > >                    say '$foo is undefined';
> > >                }
> > >
> > >                when ("foo") {
> > >                    say '$foo is the string "foo"';
> > >                }
> > >
> > >                when ([1,3,5,7,9]) {
> > >                    say '$foo is an odd digit';
> > >                    continue; # Fall through
> > >                }
> > >
> > >                when ($_ < 100) {
> > >                    say '$foo is numerically less than 100';
> > >                }
> > >
> > >                when (\&complicated_check) {
> > >                    say 'complicated_check($foo) is true';
> > >                }
> > >
> > >                default {
> > >                    die q(I don't know what to do with $foo);
> > >                }
> > >            }
> > >
> > > Muahahahahah!
> > >
> > > -scott
> > >
> > > _______________________________________________
> > > Phoenix-pm mailing list
> > > Phoenix-pm at pm.org
> > > http://mail.pm.org/mailman/listinfo/phoenix-pm
> > 
> > ---------------------------------------------------------------------
> > Michael Friedman                     HighWire Press
> > Phone: 650-725-1974                  Stanford University
> > FAX:   270-721-8034                  <friedman at highwire.stanford.edu>
> > ---------------------------------------------------------------------
> > 
> > 
> > _______________________________________________
> > Phoenix-pm mailing list
> > Phoenix-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/phoenix-pm
> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://mail.pm.org/mailman/listinfo/phoenix-pm
> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://mail.pm.org/mailman/listinfo/phoenix-pm


More information about the Phoenix-pm mailing list