in reply to Switch and $1?

$1 appears to be localised into the function that calls the regexp. In this case, that's some anonymous function created somewhere inside of Switch.pm.

If you still want to use Switch, and you can't stand the repetition, try the following:

my $dollar1; case /^\s*(\S+)$ (?{ $dollar1 = $1 })/ { ...

I don't have Switch, so I didn't test the above. Here's a proof of concept, though:

use strict; use warnings; sub do_re { my ($re, $var) = @_; $var =~ /$re/; print("inside: $1\n"); } { my $re = qr/(b)/; print("$re\n"); do_re($re, 'abc'); if (defined($1)) { print("outside: $1\n"); } else { print("outside: [undef]\n"); } } print("\n"); { my $dollar1; my $re = qr/(e)(?{ $dollar1 = $1 })/; print("$re\n"); do_re($re, 'def'); if (defined($dollar1)) { print("outside: $dollar1\n"); } else { print("outside: [undef]\n"); } } __END__ (?-xism:(b)) inside: b outside: [undef] (?-xism:(e)(?{ $dollar1 = $1 })) inside: e outside: e

I wasn't sure if it would use the lexical $dollar1 instead of the package variable by the same name, but it does.