Pre-declaring subroutines

R

RedGrittyBrick

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();
close $html or die "unable to close t.html - $!";

sub htmlhead {
my $html = <<EndOfHead;
<html>
<head><title>Test</title></head>
<body>
EndOfHead
return $html
}

sub htmltail {
my $html = <<EndOfTail;
</body>
</html>
EndOfTail
return $html
}
 
P

Paul Lalli

RedGrittyBrick said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top