Using named constants in cases of a switch

Discussion in 'Perl Misc' started by Ronny, Sep 6, 2006.

  1. Ronny

    Ronny Guest

    Assuming the following (the code should be compatible to Perl 5.8.3 AND
    Perl 6):

    use Switch 'perl6';
    use constant { FOO => 1, BAR => 2, BAZ => 3 };
    my $var = BAR;
    ...

    Now I would like to write a "switch" expression, where one of the cases
    shoulb
    be executed if $var is either BAR or BAZ:

    given($var) {
    when(FOO) { handle_foo() }
    when(??? what do I put here ???) { handle_ba() }
    else { warn "Illegal value: $var\n"; }
    }

    So the question is, how do I express "either BAR or BAZ" in the second
    "when"?

    I found one solution to this, but I don't like it: Since the argument
    of when is allowed
    to be a regexp, I could use

    when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }

    but this is slightly ugly IMO. Has someone a better solution for this?

    Note that I look for a solution using given...when. I'm aware that one
    could skin this
    cat in completely different way too, but that's not the point here.

    Ronald
    Ronny, Sep 6, 2006
    #1
    1. Advertising

  2. Ronny

    Paul Lalli Guest

    Ronny wrote:
    > Assuming the following (the code should be compatible to Perl 5.8.3 AND
    > Perl 6):
    >
    > use Switch 'perl6';
    > use constant { FOO => 1, BAR => 2, BAZ => 3 };
    > my $var = BAR;
    > ...
    >
    > Now I would like to write a "switch" expression, where one of the cases
    > shoulb
    > be executed if $var is either BAR or BAZ:
    >
    > given($var) {
    > when(FOO) { handle_foo() }
    > when(??? what do I put here ???) { handle_ba() }
    > else { warn "Illegal value: $var\n"; }
    > }
    >
    > So the question is, how do I express "either BAR or BAZ" in the second
    > "when"?


    This doesn't actually have anything to do with constants, near as I can
    tell. The situation is the same whenever you want your 'when'
    statement to be "if the given variable is any of these..."

    when ( [BAR, BAZ] ) { handle_ba() }

    Note that I think there's an error in the Switch.pm documentation, as
    the list of possible matches does not cover a given scalar with a when
    array-ref. The examples in the documentation do cover this scenario,
    however...

    Paul Lalli
    Paul Lalli, Sep 6, 2006
    #2
    1. Advertising

  3. On 2006-09-06 12:07, Ronny <> wrote:
    > Now I would like to write a "switch" expression, where one of the
    > cases shoulb be executed if $var is either BAR or BAZ:

    [...]
    > I found one solution to this, but I don't like it: Since the argument
    > of when is allowed to be a regexp, I could use
    >
    > when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }


    Urgs. What are the @{[]} for? What's wrong with

    /^(BAR|BAZ)$/

    or even

    /^BA[RZ]$/

    ?

    hp


    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Sep 6, 2006
    #3
  4. On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"
    <> wrote:

    >On 2006-09-06 12:07, Ronny <> wrote:
    >> Now I would like to write a "switch" expression, where one of the
    >> cases shoulb be executed if $var is either BAR or BAZ:

    >[...]
    >> I found one solution to this, but I don't like it: Since the argument
    >> of when is allowed to be a regexp, I could use
    >>
    >> when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }

    >
    >Urgs. What are the @{[]} for? What's wrong with
    >
    > /^(BAR|BAZ)$/
    >
    >or even
    >
    > /^BA[RZ]$/


    because of

    use constant { FOO => 1, BAR => 2, BAZ => 3 };
    Brian Greenfield, Sep 6, 2006
    #4
  5. On 2006-09-06 21:46, Brian Greenfield <> wrote:
    > On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"
    ><> wrote:
    >>On 2006-09-06 12:07, Ronny <> wrote:
    >>> Now I would like to write a "switch" expression, where one of the
    >>> cases shoulb be executed if $var is either BAR or BAZ:

    >>[...]
    >>> I found one solution to this, but I don't like it: Since the argument
    >>> of when is allowed to be a regexp, I could use
    >>>
    >>> when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }

    >>
    >>Urgs. What are the @{[]} for? What's wrong with
    >>
    >> /^(BAR|BAZ)$/

    [...]
    >
    > because of
    >
    > use constant { FOO => 1, BAR => 2, BAZ => 3 };


    Ah, yes. So that's one of the cases where the use of symbolic constants
    makes the code less instead of more readable,.

    hp

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Sep 6, 2006
    #5
  6. Ronny

    Guest

    Brian Greenfield <> wrote:
    > On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"


    >>On 2006-09-06 12:07, Ronny <> wrote:
    >>> Now I would like to write a "switch" expression, where one of the
    >>> cases shoulb be executed if $var is either BAR or BAZ:

    >>[...]
    >>> I found one solution to this, but I don't like it: Since the argument
    >>> of when is allowed to be a regexp, I could use
    >>>
    >>> when(/^(@{[BAR]}|@{[BAZ]}$/) { handle_ba() }

    >>
    >>Urgs. What are the @{[]} for? What's wrong with
    >>
    >> /^(BAR|BAZ)$/
    >>
    >>or even
    >>
    >> /^BA[RZ]$/

    >
    > because of
    >
    > use constant { FOO => 1, BAR => 2, BAZ => 3 };


    Which makes Readonly a useful alternative:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Readonly;
    Readonly my $FOO => 1;
    print $FOO;

    Axel
    , Sep 7, 2006
    #6
  7. Ronny

    Guest

    wrote:
    > Which makes Readonly a useful alternative:


    Sorry, I forgot to mention the source of this which was something I read
    by Randal Schwartz.

    Axel
    , Sep 7, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff

    65536 Switch Cases

    Bryan Parkoff, Sep 16, 2004, in forum: C++
    Replies:
    5
    Views:
    453
    Ivan Vecerina
    Sep 17, 2004
  2. z-man
    Replies:
    7
    Views:
    388
    Tor Iver Wilhelmsen
    Oct 5, 2006
  3. He Shiming

    Hundreds of cases in a switch, optimization?

    He Shiming, Jun 19, 2005, in forum: C Programming
    Replies:
    65
    Views:
    1,685
    Christian Bau
    Jun 24, 2005
  4. Lynn McGuire

    maximum cases in a switch ?

    Lynn McGuire, Aug 9, 2011, in forum: C++
    Replies:
    67
    Views:
    2,359
    James Kanze
    Aug 22, 2011
  5. Tom V.
    Replies:
    9
    Views:
    159
    Rick DeNatale
    May 7, 2007
Loading...

Share This Page