Pre-declaring subroutines

Discussion in 'Perl Misc' started by RedGrittyBrick, Aug 10, 2005.

  1. 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
    }
     
    RedGrittyBrick, Aug 10, 2005
    #1
    1. Advertising

  2. RedGrittyBrick

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 10, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rocky Moore
    Replies:
    7
    Views:
    1,799
    mikeb
    Jan 14, 2004
  2. =?Utf-8?B?S2V2aW4gQnVydG9u?=

    Pre-Send Request Headers, Pre-Send Request Content

    =?Utf-8?B?S2V2aW4gQnVydG9u?=, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,125
    =?Utf-8?B?S2V2aW4gQnVydG9u?=
    Dec 31, 2004
  3. Wladimir Borsov
    Replies:
    7
    Views:
    511
    Raymond Loman
    May 5, 2004
  4. Juha Nieminen
    Replies:
    3
    Views:
    1,210
    Juha Nieminen
    Feb 22, 2008
  5. Chris M. Thomasson
    Replies:
    10
    Views:
    671
    Nobody
    Aug 29, 2009
Loading...

Share This Page