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


In reply to Why this problem in MethodMaker ? by vishi83

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.