Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Here docs throwing syntax error - can't find string terminator

by Popcorn Dave (Abbot)
on May 29, 2007 at 22:13 UTC ( [id://618062]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks,

I'm having an issue with here docs, and I'm sure it's something simple that I'm overlooking but it's annoying me. Given this code snippet from Merlyn's website(Jan 97 column):

use strict; my $stuff = join(":", <<END =~ /(.+)/g); hi there this\tis\ta\ttest of the best! END

and this code example from CGI::AJAX

sub Show_HTML { my $html = <<EOHTML; <HTML> <BODY> Enter something: <input type="text" name="val1" id="val1" onkeyup="exported_func( ['val1'], ['resultdiv'] );"> <br> <div id="resultdiv"></div> </BODY> </HTML> EOHTML

They're both failing with the error: Can't find string terminator anywhere before EOF, but obviously both string terminators are there.

Perl version:v5.8.8 built for MSWin32-x86-multi-thread (with 33 registered patches, see perl -V for more detail)

Am I just missing something obvious here?

Thanks in advance!

Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

I would love to change the world, but they won't give me the source code

Replies are listed 'Best First'.
Re: Here docs throwing syntax error - can't find string terminator
by shmem (Chancellor) on May 29, 2007 at 22:17 UTC
    For here-doc terminators, leading blanks are whitespace is meaningful. Insert the same amount of blanks in your print statement leading delimiter, e.g.
    use strict; my $stuff = join(":", <<" END" =~ /(.+)/g); hi there this\tis\ta\ttest of the best! END

    or (better) remove the leading blanks from the end token.

    update: strikes for better wording, and changed '        END' to "        END". Well...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      You used 'END' where you should have used "END".

      print <<END, <<'END', <<"END"; abc\tdef END abc\tdef END abc\tdef END

      outputs

      abc def abc\tdef abc def

      <<END is equivalent to <<"END" and not equivalent to <<'END', so the OP needs <<"        END" rather than <<'        END'.

Re: Here docs throwing syntax error - can't find string terminator
by jdporter (Paladin) on May 29, 2007 at 23:45 UTC
Re: Here docs throwing syntax error - can't find string terminator
by FunkyMonk (Chancellor) on May 29, 2007 at 22:21 UTC
    Get rid of the whitespace before your end marker.

    ie

    my $stuff = join(":", <<END =~ /(.+)/g); hi there this\tis\ta\ttest of the best! END

    Works fine

Re: Here docs throwing syntax error - can't find string terminator
by jZed (Prior) on May 29, 2007 at 22:18 UTC
    You can't have whitespace in front of the here-doc ending marker. update : unless you do as shmem suggests
Re: Here docs throwing syntax error - can't find string terminator
by blazar (Canon) on May 29, 2007 at 22:55 UTC
    my $stuff = join(":", <<END =~ /(.+)/g); hi there this\tis\ta\ttest of the best! END

    Am I just missing something obvious here?

    Yep, 'END' ne '        END'. But incidentally IIRC Perl 6 is supposed to do something smart in those cases...

      Man do I feel silly. That never even occurred to me. Thanks!

      Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

      I would love to change the world, but they won't give me the source code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://618062]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-23 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found