vishi83 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks !!

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.

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;
##line no: 17 as per the error above.
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
    my ($self,$val1,$val2) = shift;

    You mean @_, not shift.

      Hi chromatic !

      Giving my($self, $val1, $val2) = @_ ; within a method inside Methodmaker is not working.. In the same context, Any method that takes two arguments can't be defined inside MethodMaker unless , otherwise we use array or an hash.. Why is that so ?? For example i hav a method called 'save' defined inside a MethodMaker and when i called this method like this ...

      $object->save($name,$id);
      Value of name is alone returned and id's value is null...

      Thanks Vishi
Re: Why this problem in MethodMaker ?
by jesuashok (Curate) on Oct 24, 2005 at 12:21 UTC

    Hi Vishi !!!

    I beleive that you have not done enogh debugging before posting your code regarding MethodMaker.

    As per the MethodMaker logic you can pass only one arguement for the functions defined using MethodMaker.

    For example look at the following line :- my $result = $pObj->add(100,3); Inside the child class you are calling add with 2 args. But If you try print @_ variable inside add function which is defined in Parent Clss you will get only 100 not the 3.

    So I advise you to understand that logic first why that is happening in Parent Class.

    where as in Subtract which is defined in Parent Class you are calling with only one arguemnt which is very accurate for MethodMaker Class.

    That gets wrong when you call add.

    good work anyway ....
    "Keep pouring your ideas"