Perl array attribute inside an object -
tried write perl module oop, can add object array, when use dump method, output wrong data this. error ?
thanks
bless( { '_name' => 'cusip', '_validation_array' => [], '_seq' => '1' }, 'field' );
source code:
package field; sub new { $class = shift; $self = { _name => shift, _seq => shift, _validation_array => [ @_ ], }; bless($self, $class); return $self; }; sub pushvalidation(){ $validation = shift; push(@{$self->{_validation_array}}, $validation); }; sub dump(){ foreach $validation (@{$self->{_validation_array} }) { #print dumper($validation);#will work, print $validation->{name}; #error, use of uninitialized value } } 1;
this way call method :
my $validationobj = new validation($validation->{name}, $validation->{seq}); $field->pushvalidation($validationobj);
i see several problems here, serious 1 here:
sub pushvalidation() { $validation = shift; push(@{$self->{_validation_array}}, $validation); };
this function expecting $self
argument, isn't shifting arguments. you need add use strict;
@ top of perl file. if had been enabled, issue have been obvious:
global symbol "$self" requires explicit package name @ <filename> line <line>.
same thing goes dump()
function. (by way, dump
bad method name, there (obscure) perl builtin function same name. that's not huge issue.)
Comments
Post a Comment