Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Modify the Object

by Athanasius (Chancellor)
on Oct 02, 2012 at 13:02 UTC ( #996856=note: print w/replies, xml ) Need Help??


in reply to Modify the Object

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

Replies are listed 'Best First'.
Re^2: Modify the Object
by tobyink (Abbot) on Oct 02, 2012 at 16:02 UTC

    Your example accessor has a problem:

    package Local::Class; sub new { my $class = shift; bless +{ A=>'default' }; } sub access_A { my ($self, $new_a) = @_; $self->{A} = $new_a if $new_a; return $self->{A}; } package main; use 5.010; my $obj = Local::Class->new; $obj->access_A(1); say $obj->access_A; # says 1 $obj->access_A(2); say $obj->access_A; # says 2 $obj->access_A(0); say $obj->access_A; # says 2 !!!

    A better way of writing that accessor would have been:

    sub access_A { my $self = shift; $self->{A} = shift if @_; return $self->{A}; }

    (Or even better, use Moose so that you don't need to worry about writing accessors.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: Modify the Object
by Your Mother (Chancellor) on Oct 02, 2012 at 17:06 UTC

    Your amendment is fine but I prefer the idiom tobyink showed. It's terse and lends itself to simple flow validation–

    # use Carp; sub access_A { my $self = shift; $self->{A} = shift if @_; croak "Too many arguments to access_A" if @_; $self->{A}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996856]
help
Chatterbox?
[1nickt]: I do think it is sad that roho has received 3 downvotes for his polite request, as did I when I objected to the profanity in stonecolddevin's sig. I upvoted both him and Karl for the discussion. Way too much downvoting for inappropriate reasons here!
Discipulus learn that 'argue' has a little negative sense, he thought was a neutral sense, 'vox media'
[1nickt]: argue == discuss && argue == be contentious
[Discipulus]: you are right 1nickt i didnt voted nor downvoted; I just upvote perl content i like
[1nickt]: In Spanish, to argue (like a fight) is discutir -- does not mean to discuss !
[1nickt]: Sigh, this is why I gave up human-only languages and became a Perl linguist :-)
[Discipulus]: i just, rarely, downvote unpolite posts and spam, and wrong advices
[Discipulus]: discutere in eatalian is neutral: but discutere with wife sounds implicitly negative..
[erix]: what's the offending signature?
[Discipulus]: 'give man a fish erix it sonds like a push towards omosexuality.. ;=)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-06-22 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.