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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.