Reputation: 7951
I have a Perl controller class in which I do:
sub func1 {
my $f1 = Model::myModel->new();
my $param = "test";
$f1->func2($param);
}
Model class:
sub new {
my ($class, %arg) = @_;
my $self = bless {}, $class;
return $self;
}
sub func2 {
my ($self, $param) = shift(@_);
warn $param;
}
$param
is blank. What is the mistake I am doing?
Upvotes: 0
Views: 198
Reputation: 28676
shift
only shifts the first value off of @_
. perldoc -f shift
will tell you more about how shift works.
You want:
my( $self, $param ) = @_;
You had it right in new()
. Not sure what happened ;)
Actually, FYI, your new()
will give the warning:
Odd number of elements in hash assignment
If you call it like $package->new( 'a' );
You might want to trap that, something like:
use Carp qw( croak confess );
sub new {
my $class = shift;
confess "$class requires an even number of args" if( @_ & 1 );
my %args = @_;
# ...
}
Or using whatever exception catching mechanism you use.
Upvotes: 6
Reputation: 3601
Try:
sub func2 {
my ( $self, $param ) = @_;
warn $param;
}
or
sub func2 {
my $self = shift @_;
my $param = shift @_;
warn $param;
}
Upvotes: 2