more useful options | |
PerlMonks |
Re: Difference between my $self = shift and my ($self) = @_by tobyink (Canon) |
on Feb 21, 2013 at 15:20 UTC ( [id://1019961]=note: print w/replies, xml ) | Need Help?? |
my $self = shift not only defines $self; it also modifies @_ (removing the first array item). The difference is only significant if you intend on using @_ or goto later on in the sub. Update: Personally I've recently decided to adopt a coding style where I use shift for the invocant (i.e. $self or $class; but also "conceptual invocants" like $orig in Moose around method modifiers, or $ctx in Catalyst actions) and list assignment for other parameters. For example:
For regular subs which are not intended to be called as methods, there is no invocant, so I just use list assignment to unpack all the arguments. Why did I decide to adopt this style? I find it works nicely in terms of delegating method calls and so forth where you often need to pass on @_ to other methods and functions, but without passing on the invocant. I'm not yet very strict with myself about this style, but hopefully it will become habit soon.
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
In Section
Seekers of Perl Wisdom
|
|