From: "Rob Dixon" <[EMAIL PROTECTED]>
> Jan Eden wrote:
> >
> > this is not so much a technical as a stilistic question: How do you
> > indent here-quoted parts? When writing code like this:
> >
> >     foreach $letter ('a'..'z') {
> >         my $upletter = uc $letter;
> >         print ALPHAINDEX <<"EOF";
> >         <hr />
> >         <p><a name="${letter}link"></a>
> >         <b>$upletter</b>&nbsp;&nbsp;
> >         <a href="#Navbar"><img src="../../gifs/up.gif" alt=""
> >         border="0" width="12" height="15" /></a></p> <ul> EOF
> >
> > The tabs get printed into the filehandle ALPHAINDEX. But not
> > indenting the here-quoted part makes the script less readable.
> >
> > Any suggestions? Thanks!
> 
> Hi Jan.
> 
> This is mentioned in 'Perl Cookbook' with a solution something like
> this:
> 
>   sub undent {
>     my $str = shift;
>     $str =~ s/^\s+//gm;
>     $str;
>   }
> 
>   foreach $letter ('a'..'z') {
>     my $upletter = uc $letter;
>     print ALPHAINDEX undent <<"    EOF";
>       <hr />
>       <p><a name="${letter}link"></a>
>       <b>$upletter</b>&nbsp;&nbsp;
>       <a href="#Navbar"><img src="../../gifs/up.gif" alt="" border="0"
>       width="12" height="15" /></a></p> <ul>
>     EOF
>   }

I would suggest a slightly different undent:

  sub undent {
    my $str = shift;
    $str =~ s/\A(\s*)//me;
    $str =~ s{^$1}{}gm;
    $str;
  }

The difference is that the one from cookbook removed all whitespace 
from the lines in the here-doc, while this one only removes as many 
spaces as the first line had:

        $x = undent <<'*END*';
                foo
                        bar
                        baz
                bat
*END*



Here it is without using a function: :-)

for ($x = <<'*END*') {s/\A(\s*)//me;s{^$1}{}gm};
                foo bar baz
                xxx
                 dfgdfg
                   asdfdfg
                zxcveafgdsfg
                fgdsfgdfg
*END*

print ">$x<";

Jenda

===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to