vishi83 has asked for the wisdom of the Perl Monks concerning the following question:
Look at this code below
My Parent class has two methods namely 'add' and 'subtract'. I have a problem with my 'add' ( given inside the methodmaker). It is not taking the two arguments passed to the 'add' method. See my child Class. It has got a method 'subtract' which is working fine. But when i hav the same method within a MethorMaker, I get some errors like :
Use of uninitialized value in addition (+) at Parent.pm line 17. Use of uninitialized value in addition (+) at Parent.pm line 17.
##line no: 17 as per the error above.package Parent; use strict; use warnings; use Carp; use Data::Dumper; use Class::MethodMaker get_set => [ { -read_cb => sub { ## THIS ONE IS NOT WORKING ........ +. my ($self,$val1,$val2) = shift; $self->{val1} = $val1; $self->{val2} = $val2;
my $out = $self->{val1} + $self->{val2}; return $out; } },'add', { -read_cb => sub { $_[1] - 1; } },'subtract' ], new => 'new'; 1; # Child Class ............. package Child; use Data::Dumper; use Parent; my (%val1,%val2); sub new { bless [ ] => shift; } sub subtract { $self = shift; $val1{$self}=shift; $val2{$self}=shift; return $val1{$self}-$val2{$self}; } $myobj = Child->new(); $subobj=$myobj->subtract(34,31); # this method s working print Dumper $subobj; $pObj = Parent->new(); # This is not working $pObj->add(100,3); print Dumper $pObj; $pObj=Parent->subtract(100); # subtracts 1 from 100 . print Dumper $pObj;
Please tell me wats happening in the add() method in the Parent Class ..
Anything to do with Operating Overloading ????
Thanks in Advance and awaiting your reply, Vishi
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Why this problem in MethodMaker ?
by chromatic (Archbishop) on Oct 24, 2005 at 18:53 UTC | |
by vishi83 (Pilgrim) on Oct 25, 2005 at 06:42 UTC | |
|
Re: Why this problem in MethodMaker ?
by jesuashok (Curate) on Oct 24, 2005 at 12:21 UTC |