Switch.pm
Austin Schutz
tex at off.org
Thu Jul 11 16:34:05 CDT 2002
On Thu, Jul 11, 2002 at 11:06:10AM -1000, josh hoblitt wrote:
>
> I'm trying to do some regex matching with switch then use part of the
> match in the action taken but it seems the values aren't being passed.
>
> switch ($mystr) {
> case /^(somevalue)$/m { print "$_ $1\n"; }
> .
> .
> .
> }
>
> The cases are getting matching correctly but both $1 and $_ seem to be
> empty... I suspect whats going on is that the regex is getting placed in
> a sub and then being called (haven't looked at the code for Switch.pm).
> Is there syntax that will work for me here?
>
I suspect you can't use $1 et al because they are only scoped
inside of case().
If you don't end up getting it to work, here's an equivalent that
I use, which IME works as well or better than the stuff in the switch/case
FAQ:
$foo='foobar';
$_=$foo;
SWITCH: {
/oo/ && do {
print "found foo\n";
};
/(b)a/ && do {
print "found ba\n";
print "\$1: $1\n";
# This is equivalent to 'break' in C.
last SWITCH;
};
/r/ && do {
print "r\n";
};
# Default here
}
Btw, I notice case uses the mysterious { } as a code block, like
sort() does. Can it be explained how to do this in 100 words or less? :-)
Austin
TIMTOWTDI
More information about the Pdx-pm-list
mailing list