perlmeditation
Juerd
<p>
There are some idioms that make life easier, because you'll have less to type. They're often used in one-liners and throw-away scripts. But there's one that I like more and more, every time I use it. I'm using it in production scripts now, and I'm starting to wonder if that's a good idea.
</p>
<p>
It's the cheap file slurp that uses the magical <code>*ARGV</code> and the fact that <code>(@foo, $foo) = $bar</code> will always set $foo to undef.
<code>
my $contents = do { local (@ARGV, $/) = $filename; <> };
</code>
Is this readable and maintainable enough, or do you think I should really stick to creating slurp routines?
<code>
sub slurp {
my ($filename) = @_;
local $/ = undef;
open my $fh, $filename or die "$filename: $!";
return <$fh>
}
</code>
I know [cpan://File::Slurp] exists, but don't like using modules for what can be done with a simple sub or regex. (Or maybe I would use a module if there was one module with a bunch of subs that I often use. Maybe I should release a Juerd.pm... hmmm :)
</p>
<p>
Do you think using the short slurping idiom in production code is a problem?
</p>
<p><font color=green><pre>
- Yes, I <a href="http://plp.juerd.nl/" target="_blank"><font color="green">reinvent</font></a> wheels.
- Spam: Visit <a href="http://eurotraq.com/" target="_blank"><font color="green">eurotraQ</font></a>.
</pre></font></p>