Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Unindenting here-docs (Re: My day with Damian Conway)

by tye (Sage)
on Jan 11, 2002 at 21:09 UTC ( #138052=note: print w/replies, xml ) Need Help??


in reply to Re: My day with Damian Conway
in thread My day with Damian Conway

Yes, that is fine. The problems come in when you want to strip some of the leading whitespace:

print unindent( <<" END" ); Usage: $0 [flags] [args] Gropples the snarflog. -v Verbose. -t mode Mode of groppling to use. "mode" can be: fast Minimize execution time. hard Maximize decryption time. wide Minimize alphabet size. Do not use on UTF8 files. END
Any attempt to write an unindent() for the above will run into problems in the face of 1) editors that insert tabs when maintenance work modifies the text, and 2) people who don't agree on how far apart tabstops should be set.

The only solution I've found that I like goes something like this:

sub unindent { s/^\s*\S//gm } print unindent( <<" END" ); .Usage: $0 [flags] [args] .Gropples the snarflog. . -v Verbose. . -t mode Mode of groppling to use. "mode" can be: . fast Minimize execution time. . hard Maximize decryption time. . wide Minimize alphabet size. .Do not use on UTF8 files. END

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: Unindenting here-docs (Re: My day with Damian Conway)
by tachyon (Chancellor) on Jan 31, 2002 at 12:43 UTC
    sub unindent { my $str = shift; (my $s) = $str =~ m/^(\s+)/; $str =~ s/^$s//gm; return $str; }

    This will look at the first line and extract that much leading whitespace from every line so does what you want without the .

    cheers

    tachyon

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

      Um, no, you missed the whole point.

      print unquote( <<" TEST" ); First line Second line Third line Fourth line TEST
      Tell me what that should produce and I'll tell you what my tab stops were set to and that you were wrong. :) (And don't be fooled by how your browser happens to display that code.)

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://138052]
help
Chatterbox?
[holli]: Oh, seems like LanX' team has won the fourth World Cup ... awesome!
LanX grins
[marto]: anyway, don't come to Glasgow for the football :)
[holli]: the hell? i have a scriptblocker on.
[holli]: how do you update a different window without javascript?
[holli]: ah, chatterbox right
[marto]: it's a normal HTML form, with some hidden input fields
[holli]: forgot that thing exists for a moment.
LanX welcome to the twilight zone ...
[Eily]: LanX well since the onion is your eye you should see it everywhere

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2017-12-14 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (398 votes). Check out past polls.

    Notices?