Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Using named constants in cases of a switch (http://www.velocityreviews.com/forums/t899788-using-named-constants-in-cases-of-a-switch.html)

Ronny 09-06-2006 12:07 PM

Using named constants in cases of a switch
 
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


Paul Lalli 09-06-2006 01:15 PM

Re: Using named constants in cases of a switch
 
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


Peter J. Holzer 09-06-2006 09:11 PM

Re: Using named constants in cases of a switch
 
On 2006-09-06 12:07, Ronny <ro.naldfi.scher@gmail.com> 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?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd

Brian Greenfield 09-06-2006 09:46 PM

Re: Using named constants in cases of a switch
 
On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"
<hjp-usenet2@hjp.at> wrote:

>On 2006-09-06 12:07, Ronny <ro.naldfi.scher@gmail.com> 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 };

Peter J. Holzer 09-06-2006 10:26 PM

Re: Using named constants in cases of a switch
 
On 2006-09-06 21:46, Brian Greenfield <not-for-replies@zombie.org.uk> wrote:
> On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"
><hjp-usenet2@hjp.at> wrote:
>>On 2006-09-06 12:07, Ronny <ro.naldfi.scher@gmail.com> 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?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd

axel@white-eagle.invalid.uk 09-06-2006 11:44 PM

Re: Using named constants in cases of a switch
 
Brian Greenfield <not-for-replies@zombie.org.uk> wrote:
> On Wed, 6 Sep 2006 23:11:23 +0200, "Peter J. Holzer"


>>On 2006-09-06 12:07, Ronny <ro.naldfi.scher@gmail.com> 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



axel@white-eagle.invalid.uk 09-07-2006 01:55 AM

Re: Using named constants in cases of a switch
 
axel@white-eagle.invalid.uk 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



All times are GMT. The time now is 12:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.