in reply to Confused about unary +

perl -w explains the behavior you're seeing:
print (...) interpreted as function at /tmp/t86 line 6.

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

This is an ambiguous syntax, which is why it generates a warning.

You can work around it, as you have, by putting something between the print and the parentheses, or by actually using a parenthesized call to print:

print ((split ' ', $name)[-1]);

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

    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

      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). ...