Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pre-declaring subroutines

Reply
Thread Tools

Pre-declaring subroutines

 
 
RedGrittyBrick
Guest
Posts: n/a
 
      08-10-2005
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
}
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-10-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
defining subroutines when using interpreter interactively? MackS Perl 0 03-11-2005 01:26 AM
Global subroutines fd123456 ASP .Net 6 02-04-2005 11:12 PM
Global subroutines fd123456 ASP .Net 1 01-28-2005 11:12 PM
Global subroutines tshad ASP .Net 7 01-26-2005 06:42 PM
References and subroutines ReaprZero Perl 1 12-04-2003 02:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57