Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Modify the Object

by tobyink (Abbot)
on Oct 02, 2012 at 16:02 UTC ( #996876=note: print w/ replies, xml ) Need Help??


in reply to Re: Modify the Object
in thread Modify the Object

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'


Comment on Re^2: Modify the Object
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996876]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-09-23 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (216 votes), past polls