Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: 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:

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

In reply to Re: Difference between my $self = shift and my ($self) = @_ by tobyink
in thread Difference between my $self = shift and my ($self) = @_ by Doctrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (17)
    As of 2014-04-16 20:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (434 votes), past polls