in reply to print question

So I wanted to say that

print ("a", "b")[1];

does not work, you need an extra pair of ():

print (("a", "b")[1]);

but

print qw(a, b)[1];

works fine without ().

The book I am reading(http://www.perl.org/books/beginning-perl/) says print has higher precedence than 1. But this does not work in the second example with "qw" so I am confused.

Replies are listed 'Best First'.
Re^2: print question
by Anonymous Monk on Aug 16, 2007 at 02:56 UTC
    i mean print has higher precedence than [1]. But this is not true in the second example.
      Let me try to explain the question better :

      ("a", "b") and qw(a, b)

      are two equivalent ways of defining a list, so both examples should not work(because print has higher priority than [1]):

      print ("a", "b")[1]; print qw(a, b)[1];

      But for some reason only the first one does not work...

        Your question is yet another example where using strict and warnings makes things a whole lot easier to understand:
        print ("a", "b")[1]; print qw(a, b)[1]; __END__ # Result: syntax error at x.pl line 3, near ")[" Execution of x.pl aborted due to compilation errors.

        But when you add those 2 magic lines at the top:

        use strict; use warnings; print ("a", "b")[1]; print qw(a, b)[1]; __END__ print (...) interpreted as function at x.pl line 5. Possible attempt to separate words with commas at x.pl line 6. syntax error at prog.pl line 5, near ")[" Execution of x.pl aborted due to compilation errors.

        That line about "print (...) interpreted as function ..." is what others (eg. mreece and jdporter) have already discussed.

        Another way of looking at it is that:

        print ("a", "b")[1];

        is the same, precedence-wise, as:

        (print ("a", "b"))[1];

        which doesn't make sense (ie. is a syntax error).  You have to force the desired precedence with:

        print (("a", "b")[1]);

        The line "Possible attempt to separate words with commas" is just alerting you to the fact that instead of:

        print qw(a, b)[1];

        you probably actually meant something more like:

        print qw(a b)[1];

        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        perl -Mdiagnostics -we'print (1,2)[1]' 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). syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors (#2) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of S<20 questions>. Uncaught exception from user code: syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. at -e line 1
        Hi,

        First off its qw(a b) not qw(a, b)

        Yes ("a", "b") and qw(a b) are two ways of defining the same list but saying print ("a", "b")[1] and print qw(a b)[1] are not quite the same!

        When you say print ("a", "b")[1] it looks like you're telling Perl "print my list ("a", "b")" and passing it an anonymous array whose single element is 1. That entire construct is confusing to Perl and as such it complains!

        However when you say print qw(a b)[1] you're saying to Perl "print the 2nd element of the list qw(a b)", which makes perfect sense to Perl and as such ... it obliges and prints "b" :)

        Hope that helps ;)