Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

using switch

by sicone (Initiate)
on Sep 29, 2005 at 12:56 UTC ( #496084=perlquestion: print w/ replies, xml ) Need Help??
sicone has asked for the wisdom of the Perl Monks concerning the following question:

I think I understand the switch statement, but I would like to see some examples of it in use. I want to use it with the <STDIN> function, unless there is a better way.

Comment on using switch
Re: using switch
by castaway (Parson) on Sep 29, 2005 at 13:04 UTC
Re: using switch
by Corion (Pope) on Sep 29, 2005 at 13:05 UTC
      i WAS THINKING OF USING IT IN A RPG IM WORKING ON, WELL IT'S A PROJECT I USING TO LEARN PERL. I WAS GOING TO USE THE SWITCH MODULE FOR THINGS LIKE MOVEMENT. ROUGH EXAMPLE CODE NOT TESTED.
      USE SWITCH; SUB DIRECTIONS { WHILE { $DIRECTIONS= (<STDIN>); CHOMP $DIRECTIONS $DIRECTIONS =~ TR/A-Z/a-z/; SWITCH { CASE 'NORTH' {PRINT "$HERONAME HAS LEFT NORTH"} CASE 'SOUTH' {PRINT "$HERONAME HAS LEFT SOUTH"} CASE 'EAST' {PRINT "$HERONAME HAS LEFT EAST"} CASE 'WEST' {PRINT "$HERONAME HAS LEFT WEST"} } } }
      BASICALY THATS WHAY IM TRYING TO DO...BASICALY
        Just use several if statements:
        if ($DIRECTIONS eq 'NORTH') { print "$HERONAME HAS LEFT NORTH\n" } elsif ($DIRECTIONS eq 'SOUTH') { print "$HERONAME HAS LEFT SOUTH\n" } elsif ($DIRECTIONS eq 'EAST') { print "$HERONAME HAS LEFT EAST\n" } elsif ($DIRECTIONS eq 'WEST') { print "$HERONAME HAS LEFT WEST\n" }
        Sounds like you just need to use dispatching. i.e. make a hash which contains the possible commands/directions as keys, and has strings to output as values. (Or probably more useful, has coderefs as values to be run)

        C.

Re: using switch
by thundergnat (Deacon) on Sep 29, 2005 at 13:29 UTC

    Your best bet is to avoid using Switch at all, especially in production code. It is very easy to introduce very hard to trace bugs and is just too fragile.

    If you have a large number of possible branches use a dispatch table, otherwise, if-then-else (or ? : ).

Re: using switch
by Limbic~Region (Chancellor) on Sep 29, 2005 at 13:38 UTC
    sicone,
    As has been pointed out, using Switch in production code is discouraged. This will be mostly remedied in Perl6. Dispatch Tables don't offer the same degree of flexibility, but for specific problems are a great solution.

    Cheers - L~R

Re: using switch
by Roy Johnson (Monsignor) on Sep 29, 2005 at 13:55 UTC
    There are a couple of alternatives to the Switch module, which don't share its weakness of being source-filtered. See Switch::Perlish and (my own) Case.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-01 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (13 votes), past polls