Can anybody please explain (my $self = shift) in Perl

前端 未结 3 542
长情又很酷
长情又很酷 2021-01-30 22:05

I\'m having a really hard time understanding the intersection of OO Perl and my $self = shift; The documentation on these individual elements is great, but none of

3条回答
  •  攒了一身酷
    2021-01-30 22:39

    If you call:

    $myinstance->myMethod("my_parameter");  
    

    is the same that doing:

    myMethod($myinstance, "my_parameter");  
    

    but if you do:

    myMethod("my_parameter");  
    

    only "my_parameter" wil be passed.

    THEN if inside myMethod always you do :

     $self = shift @_;  
    

    $self will be the object reference when myMethod id called from an object context
    but will be "my_parameter" when called from another method inside on a procedural way.
    Be aware of this;

提交回复
热议问题