1
1

More than 5 years have passed since last update.

shift, @_, Perl Begginer

Posted at

sometimes I forget these differences.

sub hoge {
  my $self = @_; 
sub hoge {
  my($self) = @_;
sub hoge {
  my ($self) = @_;

First of all, show the result.

  my $self = @_;
  warn "aaaaaaaaaaaaaaaaaaaaaaa";
  warn Dumper $self;
  warn "bbbbbbbbbbbbbbbbbbbbbbb";
  my($self) = @_;
  warn Dumper $self;
  warn "ccccccccccccccccccccccc";
  my ($self) = @_;
  warn Dumper $self;

I know the result, but I could not understand the reason.

my $self = @_;
# This means Receiving Value of Array by Scalar. $self is Scalar. @_ is Array.
# $self receives the number of Elements of Array.
my($self) = @_;
# This means Receiving Value of Array by Array. ($self) is array.
# So $self receives only the 1st value of @_ as Array. 

↑↑ Then I think you should use my $self = shift.

my ($self) = @_;
# same to above.
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1