o2bwise has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm doing this assignment for a perl cgi course and it called upon me to use the <<XXX string terminator. It's discussed on p. 66 of Programming Perl, 3rd edition.

I am wondering how to use it in a subroutine rather than the main portion of a program.

If I do this:
print <<GOOFY; <html> <head> <title> Chats </title> <script> function callrefresh(){ time=15; millisec = parseInt(time*1000); setTimeout('refresh()',millisec); } function refresh(){ this.location.href = "refresh.pl"; } </script> </head> <body bgcolor=dcdcdc onload=callrefresh();> GOOFY

I am fine.

However, if I do this:
sub chatOutput { print <<GOOFY; <html> <head> <title> Chats </title> <script> function callrefresh(){ time=15; millisec = parseInt(time*1000); setTimeout('refresh()',millisec); } function refresh(){ this.location.href = "refresh.pl"; } </script> </head> <body bgcolor=dcdcdc onload=callrefresh();> GOOFY }

I get an error:
Can't find string terminator "GOOFY" anywhere before EOF at <filename> line (the line that contains <<GOOFY)
How does one enable use of <<GOOFY within a subroutine?

Thanks in advance!

Tony

Replies are listed 'Best First'.
Re: use of string terminator within a subroutine
by Joost (Canon) on Aug 03, 2005 at 15:20 UTC
      Thanks!

      I don't think I'd have ever thought of it. I always indent lines in subroutines. Anyway, I placed GOOFY snug next to the left margin and it worked fine.

      Tony
Re: use of string terminator within a subroutine
by davidrw (Prior) on Aug 03, 2005 at 15:20 UTC
    From perldoc perldata: "The terminating string must appear by itself (unquoted and with no surrounding whitespace) on the terminating line." It should work fine if you just remove the leading space from the GOOFY line. Your first example also does not work for the same reason. Note that there is no difference in the usage of here-documents being in sub's or in main.
Re: use of string terminator within a subroutine
by idsfa (Vicar) on Aug 03, 2005 at 15:20 UTC

    You cannot indent the terminator.

    #WRONG: print <<FOO FOO #RIGHT: print <<BAR BAR

    For extra credit, figure out how to use a regular expression to allow for arbitrarily indented terminators.


    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. -- Cyrus H. Gordon
Re: use of string terminator within a subroutine
by bobf (Monsignor) on Aug 03, 2005 at 16:01 UTC

    apotheon wrote a tutorial on quotes in Perl, which you might find interesting (specifically the parts about HERE docs). Also in that thread, I illustrated how you can include leading whitespace before the tag, and how HERE docs can be used in code blocks without messing up the indentation.

    HTH

Re: use of string terminator within a subroutine
by Anonymous Monk on Aug 03, 2005 at 16:25 UTC
    In such cases, I just use a terminator with leading whitespace:
    sub greet { my $who = shift; print <<" EOT"; Hello, $who EOT }
A reply falls below the community's threshold of quality. You may see it by logging in.