Embrace the Little Conveniences

| 5 Comments

When Perl 6 introduced say (like print, but appends a newline) I had some skepticism.

Yes, the Modern::Perl module was as much a polemic as it was a convenience.

I know File::Slurp exists, but my fingers by now know how to read from a file in a single line of (impenetrable to the uninitiated) code:

my $text = do { local (@ARGV, $/) = $file; <> };

... and in each case, my initial feeling of "Why bother? What does that offer? How silly!" were wrong. In every one of these cases, the ability to write (and the requirement to read) less code has made my code better.

With say I don't have to worry about single- versus double-quotes, or even quoting at all sometimes. With use Modern::Perl;, I don't have to worry about enabling various features and pragmas. With File::Slurp, all I have to care about when reading from a file is typing read_file( $path ).

None of these are big deals on their own, but they're little details I don't have to worry about anymore. The same principle which says that Proc::Fork is easier to manage than writing your own forking code (I've written far too much of my own forking code) applies.

Sometimes getting the little nuisances out of the way makes me more productive and ready to tackle the big nuisances. Maybe saving my brainpower for complicated problems (what's the standard deviation from a least square fit?) is a better approach to typing my own read_file() function on every project.

As silly as it once seemed to use a CPAN module for a one liner, I've realized that not reusing good code is even sillier.

5 Comments

This is one of the main points of my YAPC::NA talk this year! In fact, File::Slurp and Proc::Fork are both on my list of essentials!

I'd better stop stealing your thunder then!

I always use Perl6::Slurp ... do you think File::Slurp is better? if so, how?

File::Slurp is more widely used than Perl6::Slurp which means that there's a much better chance that File::Slurp will already be installed on the target system than Perl6::Slurp.

File::Slurp also happens to come with some other handy functions. I've recently become a big fan of edit_file_lines, which is a bit like doing a map over each line in a file.

Interesting. I have to say I still prefer the interface of Perl6::Slurp though. But good summary!

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide

Categories

Pages

About this Entry

This page contains a single entry by chromatic published on April 25, 2012 11:55 AM.

Fund Elbow Grease, not Birthday Cake was the previous entry in this blog.

Make a DBIC Schema from DDL is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.


Powered by the Perl programming language

what is programming?