Search code examples
perlheredoc

What's the difference between these two Perl snippets?


print <<EOF
stuff
EOF
;


print <<EOF;
stuff
EOF

Why would you use one over the other?


Solution

  • Those two examples amount to the same thing behaviourally, but consider if you wanted to do something else after printing that block:

    print <<EOF
    stuff
    EOF
    . "more text here";
    

    ...or maybe you need to test the result of the operation:

    print $unstable_filehandle <<EOF
    stuff
    EOF
    or warn "That filehandle finally disappeared: $!";
    

    These examples are contrived, but you can see how sometimes it can be useful to be flexible about what code comes after the block of text.