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


In reply to When is newline not allowed as whitespace? by toma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.