http://qs321.pair.com?node_id=1054956


in reply to switch statement

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

Replies are listed 'Best First'.
Re^2: switch statement
by davido (Cardinal) on Sep 20, 2013 at 05:35 UTC

    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.

Re^2: switch statement
by tobyink (Canon) on Sep 20, 2013 at 07:39 UTC

    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
Re^2: switch statement
by ikegami (Patriarch) on Mar 18, 2014 at 13:34 UTC

    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^2: switch statement
by virtuemart2 (Novice) on Sep 23, 2013 at 06:22 UTC
    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?