in reply to assign a string using multiline format

This idiom is called the HEREDOC. Think of

<<END; sometext more END
being equivalent to "$var" where $var contains everything between the first line and the last line.

It is interpolated, so any vars inside the HEREDOC is also expanded. So

$foo = 'danmcb'; print <<END; $foo was here and here END # will print danmcb was here and here

Most wherever you can use $var, you can usually use the HEREDOC. For example

my $text = $cgi->param('text') || <<END; some text passed via the cgi END

works very well. Keep in mind, the trailing END has to be flush with the left most column.

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re^2: assign a string using multiline format
by reasonablekeith (Deacon) on Aug 26, 2005 at 12:55 UTC
    ... It is interpolated, so any vars inside the HEREDOC is also expanded

    it doesn't have to be interpolated, see the following...

    print <<'END'; $foo was here and here END # will print $foo was here and here
    ---
    my name's not Keith, and I'm not reasonable.
      why will punkish' HEREDOC interpolate while heith's won't? I don't see the difference ...?
        heredocs are interpolerated the same way, as the termination string is, so <<EOF is the same as <<"EOF", but <<'EOF' won't be interpolerated.
        read perldoc perlop -> Regexp Quote-Like Operators -> <<EOF
        my $foo = 'bar'; print <<END; 1. \$foo is $foo END print <<"END"; 2. \$foo is $foo END print <<'END'; 3. \$foo is $foo END print <<`END`; echo 4. \$foo is $foo END
        results in
        1. $foo is bar 2. $foo is bar 3. \$foo is $foo 4. $foo is bar
        See perlop#Quote-and-Quote-like-Operators.