Arguments are passed "by reference" in perl subs, allowing in-place modification. So, you can do things like the following:
sub double_it { $_[0] *= 2 } my $x = 1; print "\$x is $x\n"; double_it($x); print "\$x is now $x\n";
and have
$x is 1 $x is now 2
When you use:
my ($url, @list) = @_;
you're creating copies of the input arguments, so when you modify them you don't have any side-effect outside the function. In your code, these copies are inefficient and not needed, because the code is quite simple and you don't perform in-place modifications. So, you simply grab the first parameter, and use the resulting @_ instead of @list, saving space and time:
sub links { my $url = shift; # This removes the first element of @_ map { "$url/$_" } @_; # You don't need return :) }
I also find this construct useful when porting functions into classes. When you transform a plain function in a class/object method, the first parameter that's passed is the class name/reference to object, so if you have a function:
sub some_func { # ... some stuff? my ($foo, $bar, $baz) = @_; # ... other stuff }
you can transform it at once:
sub some_func { my $self = shift; # Maybe do some checks with $self first? # ... some stuff? my ($foo, $bar, $baz) = @_; # ... other stuff }
in a much cut-and-paste fashion. But do check the code!

Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

Don't fool yourself.

In reply to Re^3: Turning foreach into map? by polettix
in thread Turning foreach into map? by ghenry

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.