in reply to Re: Confused about unary +
in thread Confused about unary +

Sorry, I must be a bit slow on the uptake, but I still don't understand.

Perl is interpreting the print statement, followed by parentheses, as a call to print with the arguments in parentheses (print("hi there") instead of print "hi there");.

This:

print("hi there")

works perfectly (with or without strictures and warnings) with my version of perl.

Still confused

dave

Replies are listed 'Best First'.
Re: Re: Re: Confused about unary +
by sgifford (Prior) on Aug 25, 2003 at 21:16 UTC

    It's not ambiguous.

    Something like:

    print (1,2)[0];
    is ambiguous---it could be
    (print(1,2))[0];
    or
    print((1,2))[0];
    Perl's not sure which you mean, so it issues a warning (if you have warnings enabled), and arbitrarily chooses the second.

    Now this is a little bit silly, since the second interpretation is a syntax error, and you can argue that Perl should be smarter about this, but it's not, so you have to be more explicit.

    Running with warnings on will help you catch errors like this more easily; if you don't understand them, sending them to splain will help:

    $ perl -we'print (1,2)[0];' 2>&1 |splain print (...) interpreted as function at -e line 1 (#1) (W syntax) You've run afoul of the rule that says that any list op +erator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See perlop/Terms and List Operators (Leftward). ...