in reply to Uninitialized $1 in s///

This code does not generate a warning under 5.6.1, so an easy answer would be to upgrade your version of Perl.

An other way is to use the /e option is the substitution:

$b =~ s/^(?:a(\w))?(\w)$/defined $1? "$1:$2" : ":$2"/e; <>The /e option evals the right part of the s/// operator, so you can test whether $1 is defined or not.

Replies are listed 'Best First'.
Re^2: Uninitialized $1 in s///
by Aristotle (Chancellor) on Jan 02, 2002 at 20:18 UTC
    s///e is inefficient though; and this feels like a workaround more than a fix to me.

    Edit: on the other hand, it seems there really is no way to get around this on 5.6.0. The only thing I could come up with, though it seems slightly more efficient than s///e, is many times more kludgy:
    $b = $1 || "" . ":$2" if $b =~ /^(?:a(\w))?(\w)$/;
    (Which amounts to the same thing expressed in a different way..)
Re: Re: Uninitialized $1 in s///
by dmitri (Priest) on Jan 02, 2002 at 20:43 UTC
    Aha, so it's the interpreter's fault. /e is not as cute :)

    I can probably do something like

    $command = join(":", $command =~ /^(?:a(\w))?(\w)$/);

    but this requires an extra function call.