[comp.lang.perl] file handle anomaly

eversole@acae037.cadence.com (Richard Eversole; x6239) (06/28/91)

#!/util/bin/perl
#  ^-----------^---- replace with your favorite perl path
#
#  This script shows an interesting anomaly of perl
#
#  Could some one please explain why this happens and
#  why the last print is a parse error.
#
#  I kind of understand that perl is not able to determine
#  that $ofile is a file handle in the last example.
#
#  My question is: Shouldn't perl be able to correctly parse
#                   this ?
#
#  $Header: perl.c,v 4.0 91/03/20 01:37:44 lwall Locked $
#  Patch level: 0
#

$ofile = STDOUT ;

print $ofile '',<<EOD ;
This works
EOD

print STDOUT <<EOD;
This works
EOD

print $ofile <<EOD;
This returns the parse error
parse error in file bug.pl at line 30, next 2 tokens "This returns"
Execution of bug.pl aborted due to compilation errors.
EOD

-- 
  
  =====================================================================

    eversole@cadence.com
  
    Live long and prosper !

merlyn@iWarp.intel.com (Randal L. Schwartz) (06/28/91)

In article <1991Jun27.180332.6471@cadence.com>, eversole@acae037 (Richard Eversole; x6239) writes:
| print $ofile <<EOD;
| This returns the parse error
| parse error in file bug.pl at line 30, next 2 tokens "This returns"
| Execution of bug.pl aborted due to compilation errors.
| EOD

Ugh.  Consider the difference between:

	print $ofile <<EOD;
	print <<EOD;
	EOD

and

	print $ofile <<3;
	print <<EOD;
	EOD

You cannot know that the $ofile is a handle until you have seen the 3.
That's too far for the one-token-ahead parser to go.  It presumes you
mean left-shift in this case.  Try this as a workaround:

	print $ofile (<<EOD);
	print <<EOD;
	EOD

Pages 86 and 87 of the First Edition of The Book give the entire chart
of context-sensitive operators and a description of the pitfalls.

(Note the words "First Edition".... hmmm.... :-)

print "Just another Perl hacker," x (1024 >> 10)
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

flee@cs.psu.edu (Felix Lee) (06/30/91)

It's actually the tokenizer that knows the difference between
left-shift and here-is quoting.  The scanner keeps track of whether it
just saw an operator and uses this to decide what + - . * x / % & <
and << mean.
--
Felix Lee	flee@cs.psu.edu