Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

KM (4)

KM
  (email not shown publicly)
AOL IM: perlguy13 (Add Buddy, Send Message)

I wrote a book, maybe you will buy it. Writing CGI Applications with Perl [perlcgi-book.com]

Journal of KM (4)

Tuesday November 19, 2002
11:33 AM

Use shift!

[ #9021 ]
I have told my co-workers many times that using shift when getting args to a sub is what they should be doing:

sub foo { my $zog = shift; .... }

instead of:

sub foo { my $zog = $_[0]; .... }

Because no one re-uses @_, and I don't think most understand how @_ really works. Do people listen? No. Well, some did when I explained how it copies data from @_ needlessly in their code. But, it is still used.

So, today I was debugging some taint errors being throws by a CPAN module, and tracked it down to an internal module. It calls an AUTOLOAD'd method on the CPAN module, which in turn uses @_. If @_ exists, the autoloaded method returns 1, otherwise 0. But, since @_ isn't altered in the calling method, it ALWAYS returns 1 (making it useless) and becomes taint-unsafe since the autoloading tries to eval on the value from @_.

So kids, when someone explains to you that one thing isn't as good as another, please listen! It could save time from fixing broken code in the future.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Curiously, Ben Tilly mentioned that at least one company would ding you on a code review for using shift [perlmonks.org].

    Don't you think that you've just found a special case? Unless I misunderstood you, you would dislike the following:

      my ($self,$user,$pass,$action) = @_;

    For the that example, it seems clearer to use @_ rather than shift four times. It's clean and concise, though one might argue about the interface.

      my ($class, @args) = @_;

    How would you recommend using shift for that? It

    • No misunderstanding... I like shift. It's sort of like using warnings and strict.. helps protect you from stupid things that can occur (like this situation, copying large parts of data, inadvertandly having @_ diddled with unexpectedly, etc...) Also, I read code vertically, not horizontally (when scanning code), so 4 or more vertical shifts is better to me than many linear variables.

      Most things I write lately uses named params anyways. So there is only 1 or 2 shifts (2 if it is an object, for $self and the

      • KM wrote: Most things I write lately uses named params anyways.

        Hmm... perhaps you might be the target audience for what is arguably my most useless module [cpan.org] (Sub::NamedParams).

        Of course, if I ever get the courage to upload Acme::Code::Police [perlmonks.org], then that will qualify as my most useless module. I've been hesitant to do so due to potential hate mail -- not to mention lawsuits :)

        • Hmmm... looks like simply passing a hashref is easier than adding a module to pass a hashref :) But, maybe I'll take a closer peek. I tend to not like using modules to do things which are easy to do without them.

          I still have to upload my Acme::ReturnOne and Acme::ReturnZero to be the two most useless CPAN modules.... EVER!!