SPUG: Switch's "case" matching doesn't set $1 !

Tim Maher tim at consultix-inc.com
Wed Feb 14 10:15:28 PST 2007


On Wed, Feb 14, 2007 at 08:13:01AM -0500, Michael R. Wolf wrote:
> 
> Here's the bug I submitted a few weeks ago.  There's been no action on it
> since I submitted it.  I guess the anticipated fix-date is Perl6.
> 
>   http://rt.cpan.org/Public/Bug/Display.html?id=24027
> 
> Here's the code frag I submitted with the bug.  Feel free to add yours.

FWIW, I transparently handle the same scoping issues with
Shell::POSIX::Select and avoid this kind of programmer-startling
result. All it takes is to use source-code filtering to insert the
desired assignment statement into the user's program, to ensure that
$1 (for example) will wind up having the value expected.

-Tim

> #! /usr/bin/perl
> 
> use warnings;
> use Switch;
> 
> $data = 'catch 22';
> 
> switch ($data) {
>     case qr/catch (\d+)/ {
> 	print "I know my gross situation: $data\n";
> 	print "My parsed situation is unknown: $1\n";
> 	warn 'Matched, but not captured' unless defined $1;
>     }
>     else {
> 	warn "$data is unrecognized\n";
>     }
> }
> 
> 
> -- 
> Michael R. Wolf
>     All mammals learn by playing!
>         MichaelRWolf at att.net
*-------------------------------------------------------------------*
|  Tim Maher, PhD  (206) 781-UNIX   http://www.consultix-inc.com    |
|  tim at ( Consultix-Inc, TeachMePerl, or TeachMeUnix ) dot Com    |
| Classes: 2/28: Basic Perl; 3/12: Basic UNIX/Linux; 3/16: Min Perl |
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|  * "Minimal Perl" book rates 4.8 out of 5 stars at Amazon.com! *  |
| > Download chapters, read reviews, and order at MinimalPerl.com < |
*-------------------------------------------------------------------*


More information about the spug-list mailing list