Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Empty qr// fails to match -- Is this known bug?

by wokka (Acolyte)
on Oct 23, 2008 at 16:24 UTC ( #719075=note: print w/ replies, xml ) Need Help??


in reply to Empty qr// fails to match -- Is this known bug?

Check out "help perlop".

In the case of an empty regex, perl will use the last successfully-matched regex. So, in your case, it's attempting to use /(1)(2)/ on both of those strings and failing. If you do the following:

$T = qr//; $U = qr//; $_ = "12"; /(1)(2)/; my ($t, $u) = ($1, $2); $t="12"; print $t =~ /$T/ ? "pass1, " : "fail1, "; print $u =~ /$U/ ? "pass2\n" : "fail2\n";
You will see:

"pass1, fail2"

This functionality is... questionable.


Comment on Re: Empty qr// fails to match -- Is this known bug?
Download Code
Re^2: Empty qr// fails to match -- Is this known bug?
by wokka (Acolyte) on Oct 23, 2008 at 16:39 UTC
    I meant "perldoc perlop", but it doesn't matter, I somewhat misunderstood anyway. since qr// are quote operators, I think this makes perfect sense, given the way the feature is supposed to work. $T represents a compiled regex, but an empty one. When the interpreter sees that it's an empty regex, it attempts to use the last successfully matched one (as it's supposed to do).
Re^2: Empty qr// fails to match -- Is this known bug?
by mr_mischief (Monsignor) on Oct 23, 2008 at 22:59 UTC
    $T = qr//; $U = qr//; $_ = "12"; /(1)(2)/; my ($t, $u) = ($1, $2); print "t: $t\tu: $u\n"; print 'before $t gets reassigned: ' . ( ( $t =~ $T ) ? 'pass' : 'fail' ) . "\n"; $t="12"; print 'after $t gets reassigned: ' . ( ( $t =~ $T ) ? 'pass' : 'fail' ) . "\n"; print '$u never gets reassigned: ' . ( ( $u =~ $U ) ? 'pass' : 'fail' ) . "\n"; my $foo = 'bar'; print '$foo never got assigned a $1 or $2 or regex matched: ' . ( ( $foo =~ $T ) ? 'pass' : 'fail' ) . "\n"; $foo =~ /b/; print '$foo got matched against /b/: ' . ( ( $foo =~ $U ) ? 'pass' : 'fail' ) . "\n"

    This little mess produces:

    t: 1 u: 2 before $t gets reassigned: fail after $t gets reassigned: pass $u never gets reassigned: fail $foo never got assigned a $1 or $2 or regex matched: fail $foo got matched against /b/: pass

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://719075]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (16)
As of 2014-10-02 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (69 votes), past polls