in reply to Can you explain the difference with print?
G'day Bod,
I thought we'd broken your habit of omitting the strict and warnings pragmata. Perhaps not. :-(
Whenever checking out short pieces of Perl code, I typically use this alias:
$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'
[Sorry, I don't know the best way to implement that using MSWin. I'm sure another monk can advise you on that if needs be.]
$ perle 'print (localtime)[6];' print (...) interpreted as function at -e line 1. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. at -e line 1.
You can get more information from perldiag:
(W syntax) You've run afoul of the rule that says that any list operator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See "Terms and List Operators (Leftward)" in perlop.
That information may be too terse; or you may have difficulty locating it due to the presence of formats (e.g. %s in this case). Either, or both, of those situations may be resolved by using the diagnostics pragma:
$ perle 'use diagnostics; print (localtime)[6];' 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 "Terms and List Operators (Leftward)" in perlop. print (...) interpreted as function at -e line 1. 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 20 ques +tions. syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. at -e line 1.
Although you can rewrite your code:
$ perle 'my $wday = (localtime)[6]; print $wday;' 6
The quickest fix is to simply follow the function with a no-op unary plus before the left parenthesis:
$ perle 'print +(localtime)[6];' 6
See perlop: Symbolic Unary Operators:
...
Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments. ...
...
— Ken
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Can you explain the difference with print?
by Anonymous Monk on Aug 05, 2023 at 16:31 UTC | |
by kcott (Archbishop) on Aug 05, 2023 at 19:35 UTC | |
Re^2: Can you explain the difference with print?
by Bod (Parson) on Aug 05, 2023 at 23:22 UTC | |
by eyepopslikeamosquito (Archbishop) on Aug 05, 2023 at 23:47 UTC | |
by kcott (Archbishop) on Aug 06, 2023 at 04:47 UTC |