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

I'm using a dequoting function from the Perl Cookbook to make large, indented here-docs look nicer. Here's some code:
my $tempscript = dequote<<' END'; !!! some text END sub dequote { local $_ = shift; my ($white, $leader); if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) { ($white, $leader) = ($2, quotemeta($1)); } else { ($white, $leader) = (/^(\s+)/, ''); } s/^\s*?$leader(?:$white)?//gm; return $_; }

This code results in a syntax error at the end of the here-doc. The problem is that Perl doesn't know about dequote yet. Making the dequote usage more explicit (&dequote, dequote()) doesn't help in this case. Two things work: putting the dequote function before its first usage, or using a prototype.

But then things get a little more complicated. In the real code, dequote is in a utility script, separate from where it's used. I'm exporting it and useing the utilities script before trying to call the function, but I still get the syntax error. The only thing that works is if I explicitly specify the package before each call to dequote. I'd rather not do this.

I don't have problems with any other functions in my utilities script. Do functions behave differently when used in here-docs?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Prototyping functions used in here-docs
by borisz (Canon) on Jan 09, 2004 at 18:55 UTC
    You should write:
    dequote(<<' END');
    Im not sure if the spaces in front of the end are desired. Check that.
    Boris
Re: Prototyping functions used in here-docs
by Anonymous Monk on Jan 09, 2004 at 18:59 UTC

    Using parens works if you put them in the right place. And you'll need to indent your terminator the same amount as specified inside the quotes:

    my $tempscript = dequote(<<' END'); !!! some text END
Re: Prototyping functions used in here-docs
by PodMaster (Abbot) on Jan 10, 2004 at 11:08 UTC
    This code results in a syntax error at the end of the here-doc. The problem is that Perl doesn't know about dequote yet. Making the dequote usage more explicit (&dequote, dequote()) doesn't help in this case.
    It has nothing to do with dequote or prototypes. You have essentially written
    my $foo = ' ...
    which results in a syntaxt error (Can't find string terminator "'" anywhere before EOF in this case, " END" in yours).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.