Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

switch statement

by virtuemart2 (Novice)
on Sep 20, 2013 at 03:47 UTC ( #1054948=perlquestion: print w/ replies, xml ) Need Help??
virtuemart2 has asked for the wisdom of the Perl Monks concerning the following question:

As far as I know, new switch statement is introduced in version 5.10.X
http://perldoc.perl.org/5.12.5/Switch.html
http://perldoc.perl.org/5.8.9/Switch.html

I got a stupid programming question: can switch statment inside switch statement?
I mean switch inside a switch? double switch

Comment on switch statement
Re: switch statement (try switch inside switch)
by Anonymous Monk on Sep 20, 2013 at 04:06 UTC

    I got a stupid programming question: can switch statment inside switch statement?

    What happens when you try it? :P

Re: switch statement
by 2teez (Priest) on Sep 20, 2013 at 04:44 UTC

    Hi virtuemart2,

    I got a stupid programming question: can switch statment inside switch statement? I mean switch inside a switch? double switch
    One of the paragraph of one of the links you provided namely Switch.html says and I quote
    Note that switch -es can be nested within case (or any other) blocks, and a series of case statements can try different types of matches -- hash membership, pattern match, array intersection, simple equality, etc. -- against the same switch value...

    More importantly, you may want to use given/when instead of switch because old Switch used source filtering and had other limitations. Please read the Documentations of the links you provided for this.
    Lastly, you could also try this thing out and see what you get anyway.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      "you may want to use given/when instead of switch because old Switch used ..."

      I have to use old one, switch statement
Re: switch statement
by kcott (Abbot) on Sep 20, 2013 at 04:46 UTC

    G'day virtuemart2,

    Unfortunately, your post is ambiguous. This is an instance where case is important. Are you talking about switch (lowercase), as you've written twice as "switch statement"; or, are you talking about Switch (uppercase), a module you've shown two links for?

    The 'Switch' Module

    The 'switch' Feature

    Your Original Question

    As explained, the ambiguous nature of your question means I don't really know exactly what you're asking. However, by way of an answer, here's another question: "Why don't you try it yourself and see?"

    [Aside: Please see "What shortcuts can I use for linking to other information?" to post actual links instead of URLs as text.]

    -- Ken

      This is a very good answer. I think the important point that may get lost in the excellent historic detail is this:

      Switch (the module) has been deprecated and shouldn't be used in production code.

      switch (given/when) has been marked "experimental", and shouldn't be used in production code.

      Perl doesn't have a switch type statement that should be used in production code.

      perlfaq7 lists several options for "case" or "switch" statement emulation. My favorite from this document is the hash-based dispatch table. Apparently the document hasn't been updated to reflect that given/when are experimental, so just ignore that option. The others ought to work fine though. Keep in mind that in Perl's history up until 5.10 there simply didn't exist a case or switch type syntax, and people did fine without it.

      One other option

      my @dispatch = ( [ sub { shift =~ m/test/ } => sub { action } ], [ sub { shift == 2 } => sub { action } ], [ sub { shift ge 'Hello' } => sub { action } ], ); foreach my $case ( @dispatch ) { if( $case->[0]($test_value) ) { $case->[1](); # Action. last; } }

      There's a sequential dispatch table that uses callbacks for the test, and callbacks for the action. If the tests can be made uniform enough the callback wouldn't be necessary, but I used it to demonstrate a very generalized solution. I think it would become more legible by using List::MoreUtils::firstidx, but that's really just syntactic sugar; it's the same algorithm either way.

      Update: Here's a List::MoreUtils::firstidx solution (untested):

      my @dispatch = ( ... same as above ... ); $dispatch[ # In the table... firstidx { $_->[0]($test_value) } @dispatch # do a lookup... ]->[1](); # and take action.

      Hmm.... naw, that's cluttered. How about List::Util::first:

      my @dispatch = ( ...same as above ... ); ( first { $_->[0]($test_value) } @dispatch )->[1]();

      Much better. ;)


      Dave

        Switch has not been deprecated. Only its inclusion in core was deprecated. I advise against using Switch as it can result in hard to debug errors, but it's wrong to claim it has been deprecated.

      Yes, Switch is still on CPAN, but it's completely broken on recent versions of Perl. It was broken by the change in precedence for prototyped monadic functions without parentheses in Perl 5.14.

      There are patches to fix it on RT, but for whatever reason (perhaps to discourage people from using Switch), the current maintainer hasn't released a fix.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      sorry for late reply Ken.

      I talking about switch statement, like flow/control/condition that most of the programming languages have.
      I got 2 text books already (O'Reilly and Wrox) still not very helpful...

      could you please provide some examples?

      You say Switch was deprecated in 5.12.0. That's incorrect. It's inclusion in core was deprecated. The module was not deprecated.

      I advise against using Switch as it can result in hard to debug errors, but it's wrong to claim it has been deprecated.

Re: switch statement
by boftx (Chaplain) on Sep 20, 2013 at 05:39 UTC

    Depending upon what you want to do, a hash dispatch table might be your BFF in this case (bad pun intended.)

    On time, cheap, compliant with final specs. Pick two.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (17)
As of 2014-07-14 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (263 votes), past polls