Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: shift vs @_

by eyepopslikeamosquito (Canon)
on Oct 02, 2006 at 21:51 UTC ( #575965=note: print w/ replies, xml ) Need Help??


in reply to shift vs @_

This question is discussed at length in the excellent Perl Best Practices book in the third item of Chapter 9, "Argument Lists: Always unpack @_ first". Notice that this chapter is available free online as the book's sample chapter.

In this item, TheDamian argues that either version is acceptable, with the shift-based version preferred when one or more arguments needs to be sanity checked or documented with a trailing comment. The most important thing though, is to unpack them at the start of the subroutine and to avoid accessing them directly as $_[0], $_[1] etc.


Comment on Re: shift vs @_
Download Code
Re^2: shift vs @_
by shmem (Canon) on Oct 02, 2006 at 23:15 UTC
    just to prevent a perception of your statement as dogmatic -
    The most important thing though, is to unpack them at the start of the subroutine and to avoid accessing them directly as $_[0], $_[1] etc.

    - in most cases, yes. Operating directly on $_[0] is always fine if you know what you are doing, and why, e.g. the sub and the caller are designed that way to avoid costly copying. Whether in such cases a reference should be passed in the first place is another story. But then, $_[0] is a reference already...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://575965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2015-07-05 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls