It would help if you posted the code that you have a question about. What you posted doesn't do what you claim. It stringifies a sub reference, turning it into a hash key that indexes a subref as its value, and follows that up by stringifying another subref which becomes a hash key that indexes an undefined value. If warnings are enabled, it also produces one of those. There is just too much missing from the code you showed us, and what you did show us doesn't actually work... we won't be able to help with that part of the question until you fix things.

If your question is what does shift do in a subroutine, you already mostly answered it yourself.

shift shifts the lowest-indexed element off of an array, and returns that element as its return value. Inside a subroutine, the implicit array that shift operates on is @_. Inside of a subroutine, @_ contains the parameters that were passed to the sub when it is called. Therefore:

sub add { return shift() + shift(); }

...is about the same as...

sub add { return $_[0] + $_[1]; } # ...or... sub add { my ( $left, $right ) = @_; return $left + $right; }

The biggest difference being that the first and third examples disassociate the values from the caller, so that they are not aliased back to the caller.

As for what return does, well, subroutines are able to return a value to their caller. For example:

my $size = length( $some_string );

If I needed to write my own length function, here is one way to do it:

sub mylength { my $string = shift; my $size = $string =~ tr///c; # Don't worry how it works. return $size; } # Invoke it like this: my $length = mylength("Hello world."); # $length now holds 12.

return is how we tell a subroutine explicitly what value to return. Perl has another feature that dictates that if there is no explicit return statement, the last expression to be evaluated becomes the subroutine's return value. Relying on that feature is useful for really small subroutines, but for anything non-trivial can lead to confusion. So we nearly always use return for anything that isn't absolutely clear otherwise.


Dave


In reply to Re: My very confusing questions by davido
in thread My very confusing questions by ROP

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.