It's not deprecated, in fact, it's very useful. But
just because you can use a screwdriver to stir paint
doesn't mean you should (JBYCDMYS, the complement to TMTOWTDI). The thing that
cheeses me off about
shift is that it actually modifies @_. Not that I care in most cases, but it seems like wasted effort, and programs are supposed to be efficient.
I find a few cases where shift is handy, though, such as in
these highly simplified examples:
sub new
{
my $class = shift;
my $self = bless ({ @_ }, $class); # Hash-style params
return $self;
}
sub fooge_default
{
my $self = shift;
$self->fooge(@_); # Blind parameter passing
}
In most cases, though, you can just do direct assignment, mapping your params to @_ in a single declaration. If you have some sort of sick monstrosity that takes so many paramters you have to split up the declaration on to two lines, maybe you should re-think your interface. Once you're past six arguments, things get hard to parse.
As an example where
shift is dangerous, consider this:
sub fooge
{
my ($foo, $bar) = (shift, shift);
die "fooge() needs two arguments\n" unless (defined($bar));
# ...
}
What's wrong with this? What if
undef was a valid parameter? You've destroyed the evidence, so to speak. I'd try to use this instead:
sub fooge
{
my ($foo, $bar) = @_;
die "fooge() needs two arguments\n" unless (@_ == 2);
# ...
}
This version distinguishes between
undef and not provided.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.