I'm learning the intermediate perl.In that now I'm studying about the object references for class.In that they gave one package
{
package Barn;
sub new { bless [], shift }
sub add { push @{ +shift }, shift }
sub contents { @{ +shift } }
sub DESTROY {
my $self = shift;
print "$self is being destroyed...\n";
for ( $self->contents ) {
print ' ', $_->name, " goes homeless.\n";
}
}
}
in this I can't understand the work of plus sign with shift operator. In text they said ,the plus sign is like bareword it would be interpreted as a soft reference: @{"shift"}
can you anybody clearly explain its work for using plus sign with shift operator?
Without the plus sign, @{shift}
is the same as the array @shift
which doesn't call the shift
operator at all. Adding the plus sign forces shift
to be evaluated as an expression, so the shift
operator is called
I would prefer to see @{ shift() }
Methods are normally written so that they extract the first parameter to $self
, like this
sub new {
my $class = shift;
bless [ ], $class;
}
sub add {
my $self = shift;
push @$self, shift;
}
sub contents {
my $self = shift;
return @$self;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With