On Sat, Oct 13, 2001 at 04:30:08PM +0200, raptor wrote:
> I was looking at TPJ one-liners and saw this :
> 
> #32    A trick for indenting here strings
> 
> ($definition = <<'FINIS') =~ s/^\s+//gm;
>     The five varieties of camelids are the familliar
>     camel, his friends the llama and the alpaca, and
>     the rather less well-known guanaco and vicuna.
> FINIS
> 
> Courtesy of The Perl Cookbook
> 
> It is very cool if we have a way to set this RegEx so that it executes in
> compile time.... I mean if we have the ability to set this, so that we have
> any funny formating we want w/o loosing the speed of parsing it at
> runtime...

There was a big hub-bub about this back when RFCs were flying around.
If I remember Apoc 2 correctly, it will work like so:

    $definition = <<'FINIS';
        The five varieties of camelids are the familliar
        camel, his friends the llama and the alpaca, and
        the rather less well-known guanaco and vicuna.
        FINIS

The here-doc text will be stripped up to the indented terminator.  So
in this case, all the leading whitespace will be stripped off.

Not only is it a bit faster than the s/^\s+//gm regex, but it is also
more flexible.

    if( $self->feeling_snooty ) {
        print <<'POEM';
                Sometimes
                    form has to follow function
            all over the page.
        POEM
    }

Rather than simply stripping the whitespace off the front, which would
lose the layout of the poem, it only strips as much as POEM is
indented.  Like having s/^\s{4}//gm.  So you get the equivalent of:

    print
        "        Sometimes\n".
        "            form has to follow function\n".
        "    all over the page.\n";


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <[EMAIL PROTECTED]>       Kwalitee Is Job One
Home of da bomb

Reply via email to