Mailing List Archive

$1 defined if /(pattern)?/ successful?
Following up from a posting in c.l.p.m, I discovered that:
perl -e 'if ("ac" =~ /a(b)?c/) { print "Undefined\n" unless defined $1 }'
prints 'Undefined'. Neither the man pages nor the books I've seen make
any reference to this fact, for or against, but it surprised me since I
expected it to define $1 as an empty string. Note however that changing
the pattern to /a(b?)c/ does leave (defined($1) && $1 eq "").

Is this intentional behaviour? Is it desirable?

(Oh, and have a nice Thanksgiving. :)

Hugo van der Sanden
Re: $1 defined if /(pattern)?/ successful? [ In reply to ]
: Following up from a posting in c.l.p.m, I discovered that:
: perl -e 'if ("ac" =~ /a(b)?c/) { print "Undefined\n" unless defined $1 }'
: prints 'Undefined'. Neither the man pages nor the books I've seen make
: any reference to this fact, for or against, but it surprised me since I
: expected it to define $1 as an empty string. Note however that changing
: the pattern to /a(b?)c/ does leave (defined($1) && $1 eq "").
:
: Is this intentional behaviour? Is it desirable?

I suspect so.

Larry