Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Trubs with HERE docs

by Amoe (Friar)
on Sep 05, 2001 at 15:38 UTC ( [id://110253]=perlquestion: print w/replies, xml ) Need Help??

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

I have this sub:
sub help { print <<HELP; The help information goes here! HELP die(); }
That fails with "Can't find string terminator "HELP" anywhere before EOF". However, if I modify the code to this:
sub help { print <<HELP; The help information goes here! HELP die(); }
Everything goes fine. But that's really ugly. So are HERE docs compatible with indentation? How do I stop getting the error and keep my code pretty at the same time? Any help from you wise monks would be greatly appreciated. (BTW, I can't seem to find the documentation for HERE docs, so any pointers would be nice there too.)

Replies are listed 'Best First'.
Re: Trubs with HERE docs
by stefan k (Curate) on Sep 05, 2001 at 15:47 UTC

      A good answer from the perlfaqs, stefan_k++. Just let me mention in this context the nice search feature of perldoc again, the easiest way (and probably the way you did it) to find this: perldoc -q here.*doc This looks in all faqs for the regular expression /here.*doc/. And for completeness sake, this is how you search for the doc of a perl function (e.g. map): perldoc -f map

      -- Hofmator

Re: Trubs with HERE docs
by Masem (Monsignor) on Sep 05, 2001 at 15:50 UTC
    You can find more information in perlfaq4, specifically "Why don't my <<HERE documents work?". There's 3 or so solutions given on that page.

    I do understand that HERE docs will be dramatically fixed in Perl 6 to allow indententation. For the time, however, I've been using q or qq as a better replacement for HERE docs, since you don't have to worry about having the end point flush on the left margin. EG:

    sub help { print qq( The help information goes here! ) die(); }
    Alternatively, particularly if you have a lot of documentation that you'll present to the user through the perl code, you may want to look into a templating solution to remove the text of those documents from the code and into external files, but that's only a suggestion.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      The only problem with this is that you get an unwanted leading "\n" on the string ( as well as the leading whitespace common to indented herepages ).

      The good news is that this issue will be fixed in Perl 6 with indented herepage tokens allowed and automatic whitespace stripping.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Well, if you are assigning the value rather than printing it directly you can do this ugliness:
        (my $k= q( Blah Blah Blah Blah Blah Blah )) =~ s/^\s+|(?s:\s+$)//gm; print "<<$k>>\n";

        *shame*

        --
        $you = new YOU;
        honk() if $you->love(perl)

Re: Trubs with HERE docs
by dondelelcaro (Monk) on Sep 05, 2001 at 15:54 UTC
    The line orienting form of quoting (what you're doing) is documented in perldata.

    Basically, the terminating string HELP must appear by itself (unqoted and with no surrounding whitespace) on the terminating line. {Quoted verbatim from perldata}

    While it may look "pretty", it's normal for the quoted material to be non-indented. In fact, if it is indented, the type of whitespace that is present will also be output as well. (If you want it indented anyway, you need to remove it manually, ala:
    ($quote = <<'FINIS') =~ s/^\s+//gm; The Road goes ever on and on, down from the door where it began. FINIS

    {also stolen from perldata})
Re: Trubs with HERE docs
by George_Sherston (Vicar) on Sep 05, 2001 at 16:36 UTC
    If you really want the concluding HELP to indent nicely (and I am no stranger to aesthetic osbessions myself) then this should work:
    sub help { print <<' HELP'; The help information goes here! HELP die(); }
    Of course, your print text will still include the indents.

    § George Sherston
      Howdy!

      ...although if you want to interpolate variables into your here document, you need to use " instead of ' around your termination string...

      In other words:

      print <<FOO; woof woof $woof FOO
      is equivalent to
      print <<"FOO"; woof woof $woof FOO
      not
      print <<'FOO'; woof woof $woof FOO
      I've wrestled that alligator myself (aesthetic obsessions and all). At this point, I've just given up on having the here terminator indented...

      yours,
      Michael

Re: Trubs with HERE docs
by Amoe (Friar) on Sep 05, 2001 at 16:06 UTC
    Thanks for all your help. I guess I got downvoted because my question was in the FAQs. Sorry for wasting your time :)

Log In?
Username:
Password:

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

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

    No recent polls found