Everything is passed by reference in Perl.
I know it's true, but the common ways to handle subroutine arguments sort of make it untrue.
sub fiddle {
my ($arg) = @_;
$arg = "waffle";
}
sub faddle {
my $arg = shift;
$arg = "waffle";
}
sub muddle {
$_[0] = "waffle";
}
my $value = "pancake";
print "$value\n"; # Prints "pancake"
fiddle($value);
print "$value\n"; # Prints "pancake"
faddle($value);
print "$value\n"; # Prints "pancake"
muddle($value);
print "$value\n"; # Prints "waffle"
Shifting and slicing @_ into subroutine lexicals will make copies (which only matters for simple non-reference values). I know, I know. This is obvious stuff I'm talking about. But when somebody hears that everything is passed by reference in Perl, they also need to hear that it's a behavior that often gets bypassed in idiomatic code.
-
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.
|