Can some one tell how to change the values of object?
The usual way is to provide an accessor method in the First package:
sub access_A { my ($self, $new_a) = @_; $self->{A} = $new_a if defined $new_a; return $self->{A}; }
Then you can call this method in the main program as follows:
my $obj = new First('A' => 'red', 'B' => 'green'); print "Here, A's colour is ", $obj->access_A(), "\n"; $obj->access_A('yellow'); $obj->teller();
which will output:
Here, A's colour is red $VAR1 = bless( { 'A' => 'yellow', 'B' => 'green' }, 'First' );
By the way, in the definition of sub teller there should not be any parentheses after the method name. The parentheses create a prototype, saying that the sub takes no arguments — which is unnecessary, and, in this case, incorrect.
Hope that helps,
Update: Added defined to the test in sub access_A, to solve the problem identified by tobyink, below.
Athanasius <°(((>< contra mundum
In reply to Re: Modify the Object
by Athanasius
in thread Modify the Object
by sathish_cudd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |