http://qs321.pair.com?node_id=941963


in reply to Strange Regex Behavior

It might be a little more obvious like this:

$b = 'test 100'; %hash = ( a => ($b =~ /(\d+)/ ? $1 : 0), b => ($b =~ /(\w+)/ ? 1 : 0), ); print "$hash{a}\n";

which prints

test

So, it did the second pattern match first, and interpreted $1 to be the result of that pattern match. It's the same sort of ambiguity that is found in, say, ($i++)+$i.

Replies are listed 'Best First'.
Re^2: Strange Regex Behavior
by remiah (Hermit) on Dec 06, 2011 at 07:04 UTC

    I saw the same outputs in my perl 5.12.3. It seems perl confusing for $1 because this also prints "100" without warnings;

    %hash =( a => ($b =~ /(\d+)/ ? $1 : 0), b => "test b", ); print "$_=#$hash{$_}#\n" for keys %hash;

    And named capture seems to work fine.

    %hash = ( a => (($b =~ /(?<tag>\d+)/) ? $+{tag} : 0), b => (($b =~ /(?<tag>test)/) ? $+{tag} : 0), ); print "$_=#$hash{$_}#\n" for keys %hash;

    But I have no idea for why named capture doesn't confuse...

Re^2: Strange Regex Behavior
by BrowserUk (Patriarch) on Dec 06, 2011 at 06:57 UTC
    So, it did the second pattern match first, and interpreted $1 to be the result of that pattern match

    Then why does quoting $1 'fix' it?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      AFAIK, the order in which these subexpressions are evaluated is not defined. Adding one more operation (more or less any operation, "" or - or sqrt all work) does change the order of evaluation. But in the absence of some rule requiring the second and third operands of ?: to be evaluated after the first one rather than before, that's merely a detail of the implementation. I don't see any rule about it offhand in "conditional operator" in perlop.

        Hm. I believe the fact that the entire ternary expression is wrapped in parens means that the contents of those parens must be evaluated before anything outside them per perlop:

        A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses, and any function whose arguments are parenthesized.

        I also believe that as the two ternary expressions are part of a (4-term) comma separated list, those four terms should be evaluated in strictly left to right order per perlop:

        "In list context, it's just the list argument separator, and inserts both its arguments into the list. These arguments are also evaluated from left to right."
        .

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?