in reply to Re^2: What's the point of this 'overload' idiom?
in thread What's the point of this 'overload' idiom?
Is a lazy man's way of saying:
sub thing { my ($self) = @_; return $self->name; }
In other words, it's a quick way of not declaring the 'self' variable. I do it a different way if I have a one line sub:
sub name { return $_[0]->{name}; }
...the following is equivalent:
sub name { return shift->{name}; }
When a Perl method receives its parameters, the first one is always the calling object. shift or $_[0] is the same thing; the object itself. If a method is more than one line, I prefer to collect the parameter as self (eg. my ($self) = @_;). That's not always the case.
Note that shift->thing; only works once... once you've shifted the object off the stack, you can't call shift again. In these cases, $_[0] is better, but if you have to use $_[0] more than once, you're far better off for self-documenting purposes to use my ($self) = @_;.