Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Difference between my $self = shift and my ($self) = @_

by tobyink (Abbot)
on Feb 21, 2013 at 15:20 UTC ( #1019961=note: print w/replies, xml ) Need Help??

in reply to Difference between my $self = shift and my ($self) = @_

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:

use v5.14; package Person { use Moose; has first_name => (is => 'rw', isa => 'Str'); has last_name => (is => 'rw', isa => 'Str'); sub set_name { my $self = shift; my ($first, $last) = @_; $self->first_name($first); $self->last_name($last); } } package LoudNameChanges { use Moose::Role; around set_name => sub { my $orig = shift; my $self = shift; say "Changing name to: @_"; $self->$orig(@_); }; } package Person::LoudNameChanges { use Moose; extends 'Person'; with 'LoudNameChanges'; } my $bob = Person::LoudNameChanges->new( first_name => 'Robert', last_name => 'Smith', ); $bob->set_name('David', 'Jones'); print $bob->dump;

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019961]
[Corion]: Also, I think the OPs problem is on their intranet and not on the internet (also, they should likely use WWW::Mechanize instead, which knows about cookie)
[1nickt]: yes, could be.
[1nickt]: I don;t see the link , definitely, after reloading the node
[1nickt]: Is it possible to disable it?
[Discipulus]: if you mean [download] after the code it is there 1nickt
[1nickt]: yes, Corion sees it too, and I see it when not logged in.
[Corion]: I think you can disable download links in your settings?
[1nickt]: Looking in settings to see if I disabled it in my profile ...
[1nickt]: There is "Don;t show embedded d/l links" in Display settings. It is unchecked.
[1nickt]: Oh, there is a minimum lines setting!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-05-23 18:19 GMT
Find Nodes?
    Voting Booth?