RedGrittyBrick wrote:
> I've tried various perldoc incantations but haven't yet found where it
> is it explained *why* it is necessary to declare subroutines in advance
> of using them in order to avoid the following warning messages:
>
> I thought the parentheses would identify the name as a subroutine
> invocation.
>
> Can some kind person point out TFM for me to R?
>
>
> C:\Temp>perl t2.pl
> Bareword found where operator expected at t2.pl line 10, near "$html
> htmlhead"
> (Missing operator before htmlhead?)
> Bareword found where operator expected at t2.pl line 12, near "$html
> htmltail"
> (Missing operator before htmltail?)
>
> C:\Temp>type t2.pl
> #!perl
>
> use strict;
> use warnings;
>
> # sub htmlhead; # uncommenting these makes the warnings go away.
> # sub htmltail;
>
> open my $html, '>', 't.html' or die "unable to open t.html - $!";
> print $html htmlhead();
> print $html " foo\n";
> print $html htmltail();
perldoc -f print
print FILEHANDLE LIST
print LIST
print Prints a string or a list of strings. Returns true
if successful. FILEHANDLE may be a scalar variable
name, in which case the variable contains the name
of or a reference to the filehandle, thus
introducing one level of indirection. (NOTE: If
FILEHANDLE is a variable and the next token is a
term, it may be misinterpreted as an operator unless
you interpose a "+" or put parentheses around the
arguments.)
So basically, without the subroutine declarations, Perl thinks maybe
you're trying to have
$html htmlhead
be the FILEHANDLE, and () be the LIST to print. It then tells you that
htmlhead is a bareword, and perhaps you missed an operator between
$html and htmlhead.
With the subroutine declarations, Perl knows that htmlhead is a
subroutine call, not a bareword.
Seems like a mistake in the parsing, personally. But there you have
it.
Paul Lalli
|