in reply to Re: Regular Expression Question
in thread What happens with empty $1 in regular expressions? (was: Regular Expression Question)

No, $1 will not be undef if the match fails. The only time they are undefined is when a match has not yet been done, or a match does not contain a captured pattern for that variable:
#!/usr/bin/perl -w if ("abc" =~ /(\w+)/, 1) { print "abc => $1\n" } { print "local: $1\n"; if ("ghi" =~ /\w+/, 1) { print "ghi => $1\n" } if ("def" =~ /(\w+)/, 1) { print "def => $1\n" } if ("[=]" =~ /(\w+)/, 1) { print "[=] => $1\n" } } print "general: $1\n"; __END__ abc => abc local: abc Use of uninitialized value at regexes line 6. ghi => def => def [=] => def general: abc
However, a failed match returns false, so if you removed the , 1's from each of those, you wouldn't see the line for "=".

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Re: Regular Expression Question
by ZZamboni (Curate) on Feb 28, 2001 at 19:40 UTC
    I agree completely with you, and that's what I meant, that if the regex does not match, $1 is not modified in any way (neither set or unset). But your example made it way clearer than any explanation :-)

    --ZZamboni