passing argument to a subroutine

Discussion in 'Perl Misc' started by ccc31807, Dec 3, 2009.

  1. ccc31807

    ccc31807 Guest

    If you call a sub like this:
    some_sub($arg);

    You can copy the argument like this:
    sub some_sub
    {
    my $arg_copy = shift;
    ...
    }

    However, I've noticed that some BIG NAMES in the Perl community do
    this:
    my ($arg_copy) = @_;

    Is there any real difference between the two? If so, what is the
    difference?

    CC.
     
    ccc31807, Dec 3, 2009
    #1
    1. Advertising

  2. ccc31807 <> wrote:
    >If you call a sub like this:
    >some_sub($arg);
    >
    >You can copy the argument like this:
    >sub some_sub
    >{
    > my $arg_copy = shift;
    > ...
    >}
    >
    >However, I've noticed that some BIG NAMES in the Perl community do
    >this:
    > my ($arg_copy) = @_;
    >
    >Is there any real difference between the two? If so, what is the
    >difference?


    The first version consumes the first argument, i.e. it is gone from @_
    after the shift.
    The second version preserves the first argument, i.e. it is still
    present in @_ after the assignment.

    jue
     
    Jürgen Exner, Dec 3, 2009
    #2
    1. Advertising

  3. ccc31807

    Willem Guest

    ccc31807 wrote:
    ) If you call a sub like this:
    ) some_sub($arg);
    )
    ) You can copy the argument like this:
    ) sub some_sub
    ) {
    ) my $arg_copy = shift;
    ) ...
    ) }
    )
    ) However, I've noticed that some BIG NAMES in the Perl community do
    ) this:
    ) my ($arg_copy) = @_;
    )
    ) Is there any real difference between the two? If so, what is the
    ) difference?

    Technically speaking, the second form preserves the @_ array.

    But I think the main reason is for consistency. Think about
    the two above conventions in the case of, say, a five-arg function.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Dec 3, 2009
    #3
  4. >>>>> "ccc31807" == ccc31807 <> writes:

    ccc31807> Is there any real difference between the two? If so, what is the
    ccc31807> difference?

    I use:

    my $self = shift;

    or

    my $class = shift;

    on instance and class methods, simply because the first parameter
    is special. For the remaining arguments, I'm all over the map. :)

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
     
    Randal L. Schwartz, Dec 3, 2009
    #4
  5. On Thu, 03 Dec 2009 18:29:58 +0100, Randal L. Schwartz
    <> wrote:

    >>>>>> "ccc31807" == ccc31807 <> writes:

    >
    > ccc31807> Is there any real difference between the two? If so, what is
    > the
    > ccc31807> difference?


    The difference is that "shift" is changing the array, while the assignment
    does not.

    So, if you're pedantic, you might ask why all those bytes have to be
    shifted around just to get at the first argument.

    I prefer "my ($a,$b,$c)=@_;" as well, because it does exactly what is
    meant without any side effects or superflous shuffling of RAM. Also, it
    looks *just* like the argument declarations in other languages ("void
    xyz(int a,int b,int c)"), so is very clear to understand.

    In class methods, I use

    sub fn
    {
    my $self=shift @_;
    my ($args...)=@_;

    for no particular technical reason except that by doing this, after the
    shift, @_ looks just like what the caller saw.
     
    Jochen Lehmeier, Dec 3, 2009
    #5
  6. ccc31807

    Willem Guest

    Jochen Lehmeier wrote:
    ) In class methods, I use
    )
    ) sub fn
    ) {
    ) my $self=shift @_;
    ) my ($args...)=@_;
    )
    ) for no particular technical reason except that by doing this, after the
    ) shift, @_ looks just like what the caller saw.

    And, additionally, you can do $self->foobar(@_) which (I think ?) will
    find the method in a child object which inherited your package.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Dec 3, 2009
    #6
  7. ccc31807

    Dr.Ruud Guest

    Willem wrote:
    > Jochen Lehmeier wrote:


    > ) In class methods, I use
    > )
    > ) sub fn
    > ) {
    > ) my $self=shift @_;
    > ) my ($args...)=@_;
    > )
    > ) for no particular technical reason except that by doing this, after the
    > ) shift, @_ looks just like what the caller saw.
    >
    > And, additionally, you can do $self->foobar(@_) which (I think ?) will
    > find the method in a child object which inherited your package.


    Variant:

    shift->foobar(@_)

    --
    Ruud
     
    Dr.Ruud, Dec 3, 2009
    #7
  8. On Thu, 03 Dec 2009 21:43:38 +0100, Ben Morrow <> wrote:

    >
    > Quoth "Jochen Lehmeier" <>:
    >> On Thu, 03 Dec 2009 18:29:58 +0100, Randal L. Schwartz
    >> <> wrote:
    >>
    >> >>>>>> "ccc31807" == ccc31807 <> writes:
    >> >
    >> > ccc31807> Is there any real difference between the two? If so, what is
    >> > the
    >> > ccc31807> difference?

    >>
    >> The difference is that "shift" is changing the array, while the
    >> assignment
    >> does not.
    >>
    >> So, if you're pedantic, you might ask why all those bytes have to be
    >> shifted around just to get at the first argument.

    >
    > All which bytes? shift on @_ is special-cased, since it's so common, to
    > do nothing more than adjust a pointer.


    I love perl.

    Performance aside, let me rephrase my answer:

    "So, if you're pedantic, you might ask why the @_ array should be
    modified just to get at the first argument."

    Which is be the reason why I (and possibly others) use "my (...)=@_"
    instead of "my $x=shift @_".
     
    Jochen Lehmeier, Dec 3, 2009
    #8
  9. "Jochen Lehmeier" <> wrote:
    >The difference is that "shift" is changing the array, while the assignment
    >does not.
    >
    >So, if you're pedantic, you might ask why all those bytes have to be
    >shifted around just to get at the first argument.


    What bytes do you mean when talking about "bytes have to be shifted
    around"?

    >I prefer "my ($a,$b,$c)=@_;" as well, because it does exactly what is
    >meant without any side effects or superflous shuffling of RAM. Also, it


    What shuffling of RAM are you talking about?

    jue
     
    Jürgen Exner, Dec 3, 2009
    #9
  10. "Jochen Lehmeier" <> wrote:
    >"So, if you're pedantic, you might ask why the @_ array should be
    >modified just to get at the first argument."


    A valid question. And depending upon your style an answer may be:

    Because I got the first argument, I dealt with it, and now I want to
    move on to the next argument.

    This is particularly useful when you are dealing with a list of unknown
    length, let's say an arbitrary number of filenames and each file needs
    to be treated the same. Then just loop over shift(@_) until @_ is empty.

    >Which is be the reason why I (and possibly others) use "my (...)=@_"
    >instead of "my $x=shift @_".


    Which is IMO a nice way to assign a meaningful name to each argument for
    a known number of arguments.

    jue
     
    Jürgen Exner, Dec 3, 2009
    #10
  11. ccc31807

    Guest

    On Thu, 3 Dec 2009 20:43:38 +0000, Ben Morrow <> wrote:

    >
    >Quoth "Jochen Lehmeier" <>:
    >> On Thu, 03 Dec 2009 18:29:58 +0100, Randal L. Schwartz
    >> <> wrote:
    >>
    >> >>>>>> "ccc31807" == ccc31807 <> writes:
    >> >
    >> > ccc31807> Is there any real difference between the two? If so, what is
    >> > the
    >> > ccc31807> difference?

    >>
    >> The difference is that "shift" is changing the array, while the assignment
    >> does not.
    >>
    >> So, if you're pedantic, you might ask why all those bytes have to be
    >> shifted around just to get at the first argument.

    >
    >All which bytes? shift on @_ is special-cased, since it's so common, to
    >do nothing more than adjust a pointer.
    >

    [snip]
    >
    >Ben


    Hey Ben. I'm not really sure but since Perl doesn't seem to give anything
    back, adjusting the pointer would seem all it does on "shift,pop" .. and
    "unshift,push,splice" if there is room to expand at the boundries without
    needing more memory.

    For example unshift grows down, push grows up, shift shrinks up, pop shrinks
    down via head/tail pointers.

    Of course, this is if internally, a Perl array is really an array of pointers
    to structures with pointers to data grabbed from a free pool that is managed by Perl.
    Its hard to fathom it could be done any other way with typless data (elements) but
    I'm sure there is optimizations or some hybrid methods being used.

    And within the array's current block of pointers, anything that can be moved
    around without re-allocation is prefferable, say from using splice.

    I don't know for sure, I'm sure there is some paper on implementing a typeless
    language, but if shift @array did a re-alloc then move every time, or just a
    move, then performance would really fall off.

    -sln
     
    , Dec 4, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Trans
    Replies:
    3
    Views:
    208
    Robert Klemme
    Jan 26, 2005
  2. ±è ÁØ¿µ
    Replies:
    7
    Views:
    267
    Einar Magnús Boson
    Nov 27, 2008
  3. Replies:
    0
    Views:
    113
  4. king
    Replies:
    5
    Views:
    207
  5. User1014
    Replies:
    1
    Views:
    200
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page