toma has asked for the wisdom of the Perl Monks concerning the following question:
This code runs properly:
#!/usr/bin/perl open (T, ">out") or die "Can't open out $!\n"; printf(T "hello\n");
But this code:
#!/usr/bin/perl open (T, ">out") or die "Can't open out $!\n"; printf( T "hello\n");
Prints the warning message:
String found where operator expected at ./t.pl line 4, near "T "hello\ +n"" (Do you need to predeclare T?)
If I add use diagnostics I get this message in addition:
(S) The Perl lexer knows whether to expect a term or an operator. + If it sees what it knows to be a term when it was expecting to see an operator, it gives you this warning. Usually it indicates that an operator or delimiter was omitted, such as a semicolon.
I expect whitespace to be benign here, and I don't expect this message. If I wanted this much meaning in the whitespace, I'd be using python! My code these days doesn't look much like this, but perltidy formatted it this way.
Next I tried adding a space like this:
#!/home/toma/perl58i/bin/perl use strict; use warnings; use diagnostics; open (T, ">out") or die "Can't open out $!\n"; printf ( T "hello\n");
and I got this additional diagnostic:
printf (...) interpreted as function at ./t.pl line 6 (#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).
Removing the parenthesis like this:
#!/usr/bin/perl open (T, ">out") or die "Can't open out $!\n"; printf T "hello\n";
also fixes it. Why?
It should work perfectly the first time! - toma
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: When is newline not allowed as whitespace?
by Zaxo (Archbishop) on Dec 22, 2002 at 05:46 UTC | |
Re: When is newline not allowed as whitespace?
by theorbtwo (Prior) on Dec 22, 2002 at 06:42 UTC | |
Re: When is newline not allowed as whitespace?
by Dogma (Pilgrim) on Dec 23, 2002 at 06:11 UTC |