It isn't unusual to see something like
my ($self) = @_;,
but I've found that using
my $self = shift; is
more flexible. After all, if you need this in one function,
you need it in every one, so it is easy to cut-and-paste.
That being said, the reason you are able to modify "$self"
is because you are not really modifying it. You are modifying
the data that it "refers" to. It's a reference, which is
really like a pointer. It merely indicates where data is
stored, instead of actually storing data. This is why you
need to use the arrow operator to "dereference" it and
get the goods.
So, if you had code like:
sub X
{
my $self = shift;
$self->{age} = 17;
}
Then, as you expect, the 'age' value of your $self-referenced
object becomes 17. If you did something like this:
sub X
{
my $self = shift;
print "\$self = $self\n";
$self->{age} = 17;
print "\$self = $self\n";
}
You will note that $self does not change in the function,
and that in both cases it is clearly a reference to a HASH.
This is something different:
sub Z
{
my ($foo) = @_;
print "\$foo = $foo\n";
$foo = 17;
print "\$foo = $foo\n";
}
my $bar = 29;
print "\$bar = $bar\n";
Z($bar);
print "\$bar = $bar\n";
In this case, $bar is not modified, though a copy of it,
called $foo, is. Here's an example that uses a reference
instead:
sub Z
{
my ($foo) = @_;
print "\$foo = $foo\n";
$$foo = 17;
print "\$foo = $foo\n";
}
my $bar = 29;
print "\$bar = $bar\n";
Z(\$bar);
print "\$bar = $bar\n";
On the way into the function, the variable is prefixed with
a backslash, meaning "pass by reference". Inside the
function, the scalar is "used" with a second $, meaning
that you are modifying the value, not the reference. This
is another form of dereferencing, not unlike the "arrow
operator" used by arrays and hashes.
Any time you see an arrow operator, think of it like a
pointer, which it is supposed to represent visually. It
points to something which is elsewhere.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.