Using named constants in cases of a switch

R

Ronny

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
 
P

Paul Lalli

Ronny said:
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
 
P

Peter J. Holzer

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
 
B

Brian Greenfield

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 };
 
P

Peter J. Holzer

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
 
A

axel

Brian Greenfield said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top