Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: What's the point of this 'overload' idiom?

by stevieb (Canon)
on Dec 07, 2022 at 07:33 UTC ( #11148643=note: print w/replies, xml ) Need Help??


in reply to Re^2: What's the point of this 'overload' idiom?
in thread What's the point of this 'overload' idiom?

shift->name

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) = @_;.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11148643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2023-04-01 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?