Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by Cody Fendant (Hermit)
on Dec 07, 2022 at 07:01 UTC ( [id://11148642]=note: print w/replies, xml ) Need Help??


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

Thanks for that. If you don't mind, what's the point of the specific thing where it's not a code reference in there but shift->name;?

Replies are listed 'Best First'.
Re^3: What's the point of this 'overload' idiom?
by stevieb (Canon) on Dec 07, 2022 at 07:33 UTC
    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://11148642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found