Tuesday, June 27, 2006

Perl: Here documents

Perl: Here documents

I was hacking a bit of perl tonight and didn't quite recall how to compose a here document in perl.

In typical perl fashion, as the article above shows, there's about 5 different ways to do here documents in perl, most of which are very cool.

For example, did you know you could do this?

print <<`FOO`
 echo "Hello World" | mail -s "Test mail..." benjisimon@nowhere.com
FOO

Or how about this?

%data = <<END =~ /(\w+): (.*)/g;
fred: Fred Flintstone
barney: Barney Rubble
betty: Betty Rubble
wilma: Wilma Flintstone
END

The second example is especially interesting because it allows you to keep a nicely formatted description of your data in your perl source code, yet transparently load it into a hash.

There you have it - the kitchen sink of here documents.

No comments:

Post a Comment