SPUG: Re: Filter::Simple by J.P. tomorrow too

Tim Maher tim at consultix-inc.com
Tue Apr 16 10:21:40 CDT 2002


On Tue, Apr 16, 2002 at 07:40:03AM -0700, Richard Anderson wrote:
> What's wrong with this (paraphrased from my memory of the Camel book section
> on here docs, may not have the syntax right):
> 
> ($test = <<END ) =~ s/^\s+//;
>                  This is
>                   my indented
>                   here document.
> END
> 
> Cheers,
> Richard
> richard at richard-anderson.org
> www.richard-anderson.org
> www.raycosoft.com

The whole gruesome story on kludging Here-Docs to act nicely follows.
Enjoy! 8-}

-Tim
======================================================
|  Tim Maher, Ph.D.            tim at timmaher.org      |
|  SPUG Founder & Leader       spug at seattleperl.org  |
|  Seattle Perl Users Group    www.seattleperl.org   |
======================================================

$ perldoc -tq document


Found in /usr/lib/perl5/5.6.1/pod/perlfaq4.pod

  Why don't my <<HERE documents work?

            Check for these three things:

            1. There must be no space after the << part.
            2. There (probably) should be a semicolon at the end.
            3. You can't (easily) have any space in front of the tag.
            If you want to indent the text in the here document, you can do
            this:

                # all in one
                ($VAR = <<HERE_TARGET) =~ s/^\s+//gm;
                    your text
                    goes here
                HERE_TARGET

            But the HERE_TARGET must still be flush against the margin. If
            you want that indented also, you'll have to quote in the
            indentation.

                ($quote = <<'    FINIS') =~ s/^\s+//gm;
                        ...we will have peace, when you and all your works have
                        perished--and the works of your dark master to whom you
                        would deliver us. You are a liar, Saruman, and a corrupter
                        of men's hearts.  --Theoden in /usr/src/perl/taint.c
                    FINIS
                $quote =~ s/\s*--/\n--/;

            A nice general-purpose fixer-upper function for indented here
            documents follows. It expects to be called with a here document
            as its argument. It looks to see whether each line begins with a
            common substring, and if so, strips that substring off.
            Otherwise, it takes the amount of leading whitespace found on
            the first line and removes that much off each subsequent line.

                sub fix {
                    local $_ = shift;
                    my ($white, $leader);  # common whitespace and common leading string
                    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 works with leading special strings, dynamically determined:

                $remember_the_main = fix<<'    MAIN_INTERPRETER_LOOP';
                    @@@ int
                    @@@ runops() {
                    @@@     SAVEI32(runlevel);
                    @@@     runlevel++;
                    @@@     while ( op = (*op->op_ppaddr)() );
                    @@@     TAINT_NOT;
                    @@@     return 0;
                    @@@ }
                MAIN_INTERPRETER_LOOP

            Or with a fixed amount of leading whitespace, with remaining
            indentation correctly preserved:

                $poem = fix<<EVER_ON_AND_ON;
                   Now far ahead the Road has gone,
                      And I must follow, if I can,
                   Pursuing it with eager feet,
                      Until it joins some larger way
                   Where many paths and errands meet.
                      And whither then? I cannot say.
                            --Bilbo in /usr/src/perl/pp_ctl.c
                EVER_ON_AND_ON


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list