Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the 'Use of "shift" without parentheses is ambiguous' warning issued by Perl?

Does anyone know what parsing or precedence decisions resulted in the warning 'Use of "shift" without parentheses is ambiguous' being issued for code like:

shift . 'some string';

# and not

(shift) . 'some string'; # or
shift() . 'some string';

Is this intentional to make certain syntactic constructs easier? Or is it merely an artifact of the way perl's parser works?

Note: this is a discussion about language design, not a place to suggest

"@{[shift]}some string"
like image 667
Eric Strom Avatar asked Oct 18 '09 03:10

Eric Strom


2 Answers

With use diagnostics, you get the helpful message:

    Warning: Use of "shift" without parentheses is ambiguous at (eval
        9)[/usr/lib/perl5/5.8/perl5db.pl:628] line 2 (#1)
    (S ambiguous) You wrote a unary operator followed by something that
    looks like a binary operator that could also have been interpreted as a
    term or unary operator.  For instance, if you know that the rand
    function has a default argument of 1.0, and you write

        rand + 5;

    you may THINK you wrote the same thing as

        rand() + 5;

    but in actual fact, you got

        rand(+5);

    So put in parentheses to say what you really mean.

The fear is you could write something like shift .5 and it will be parsed like shift(0.5).

like image 64
mob Avatar answered Nov 08 '22 23:11

mob


Ambiguous doesn't mean truly ambiguous, just ambiguous as far as the parser had determined. shift . in particular is "ambiguous" because . can start a term (e.g. .123) or an operator, so it doesn't know enough to decide whether what follows is shift's operand or an operator for which shift() is the operand (and the parser isn't smart enough to know that: a) the . isn't the start of such a term or b) .123 isn't a valid operand for shift).

like image 8
ysth Avatar answered Nov 09 '22 01:11

ysth