[Dub-pm] 'Shortening' code.

Dave O Connor doc at redbrick.dcu.ie
Sat Apr 24 03:56:20 CDT 2004

Grant McLean said on Sat, Apr 24, 2004 at 08:09:48PM +1200:
> If I might play devil's apricot, isn't that exactly the purpose of
> higher level languages?  After all, if execution time was all-important,
> we'd all be writing in assembler.  One of the reasons I love Perl is
> that it allows me to achieve a lot without having to type a lot.

I guess I should have mentioned readability, too. While it can't immediately
be said that a one-liner is slower (in fact, it's probbaly faster, unless
you've used a module just to squash it down from several lines), it can
usually be said that it's less readable. For example, the author of the
module gives the following example for slurping a file:

open my $file_handle, './Scotty' or die "Scotty can't be slurped:\n$!";
local $/;   # Set input to "slurp" mode.
my $big_string = <$file_handle>;
close $file_handle;

An equivalent (and more readable way is:

open(FH,"Scotty") or die "Scotty can't be slurped:\n$!";
my $big_string = join('',<FH>);

While this is more 'idiomatic', it's still obvious to someone with a good
grounding in perl what is going on here, and it's definitely not worth writing
a module for to 'simplify' your code down the the almighty one-liner :)

	- DoC

More information about the Dublin-pm mailing list