in reply to Unexpected matching results

It's delayed evaluation of $1. The solution is to put it in quotes, i.e. "$1".

He's a shorter example:

$_ = '1a'; print( (/(\d)/ ? $1 : 0), (/(\D)/ ? $1 : 0), "\n"); #aa print( (/(\d)/ ? "$1" : 0), (/(\D)/ ? "$1" : 0), "\n"); #1a
At run time, the variable $1 is pushed on the argument stack twice, then print is called. Print evaluates each of its arguments, getting its string value, and at this point $1 evaluates to the first capture of the last pattern match.

Think of $1 being tied, with FETCH() being called (twice) from print.

Dave.

Replies are listed 'Best First'.
Re^2: Unexpected matching results
by hdb (Monsignor) on Sep 06, 2013 at 07:51 UTC

    Thanks a lot! Now I (think I) understand it.