seriousfun has asked for the wisdom of the Perl Monks concerning the following question:

c++ offers a case command which is great for menus and such. Is there a similar command in perl?

Thanks

Replies are listed 'Best First'.
Re: case command ??
by Juerd (Abbot) on Dec 30, 2001 at 05:53 UTC
    Know thy FAQs :)

    This specific question can be found in perlfaq7 under "How do I create a switch or case statement". It even has 4 examples.

    I recommend reading all of the FAQs. You can just scan through the headlines and forget the answers - so when you need an answer, you'll know where to find it.

    perldoc -q can be very handy. In this case, perldoc -q case or perldoc -q switch would have found the answer for you.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: case command ??
by Zaxo (Archbishop) on Dec 30, 2001 at 05:33 UTC

    No, but 'perldoc perlsyn' shows a numbers of ways to do it, not including the great fun of a dispatch table -- a Hash of Subs.

    After Compline,
    Zaxo

Re: case command ??
by IlyaM (Parson) on Dec 30, 2001 at 05:36 UTC
    As Zaxo said there is no builtin case command. However with module Switch you can use switch/case syntax in your Perl programs.

    --
    Ilya Martynov (http://martynov.org/)

Re: case command ??
by grep (Monsignor) on Dec 30, 2001 at 05:37 UTC
    Not specifically, but you can emulate it. I use this technique for dispatch tables.

    #!/usr/bin/perl -w use strict; my $myvar = "that_one"; $_ = $myvar; CASE: { /^this_one$/ && do { print localtime(); last CASE; }; /^that_one$/ && do { print "SPOOOON!\n"; last CASE; }; }

    UPDATE: As Juerd has pointed out, this is a FAQ. If you take my answer from here please read the FAQ also. It is so much better to know where to find the information than to just know it. Programming is only fun when you are constantly learning, and you can't learn without the proper resources.
    grep
    grep> cd pub grep> more beer
Re: case command ??
by Steve_p (Priest) on Dec 30, 2001 at 10:53 UTC
    You can get the Switch module from CPAN (or via ppm if you are using ActiveState).