toma has asked for the wisdom of the Perl Monks concerning the following question:

I was fixing up some of my old perl code from 1993, adding use strict, running perltidy, etc, when I ran across a problem with whitespace in perl5.6.1 and perl5.8.0.

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

    It would probably take a cold wet dive into toke.c to figure this out in detail.

    As a matter of perl semantics, a handle in *print* is an indirect object, and not properly part of the argument list. Calling as printf(T @foo); runs the risk of misinterpretation, whils T->printf @foo; or printf T (@foo); does not.

    After Compline,
    Zaxo

Re: When is newline not allowed as whitespace?
by theorbtwo (Prior) on Dec 22, 2002 at 06:42 UTC

    Zaxo++. That said, when you specify things that should be illegal, like print(T @foo), perl often tries to DWIM in ways more hurestic then definitional -- by defintion, that should be print(T(@foo)), but since there isn't a sub T, it guesses that you might have meant an inderect object, most of the time. Perl's mostly only whitespace sensitive in rare cases, where there are more then one valid way to parse your code.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: When is newline not allowed as whitespace?
by Dogma (Pilgrim) on Dec 23, 2002 at 06:11 UTC
    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.

    You should checkout the SuperPython module on CPAN. (for a laugh).

    M/MJ/MJD/SuperPython-0.91.tar.gz

    Cheers,

    -J