scorpio17 has asked for the wisdom of the Perl Monks concerning the following question:
But what I REALLY want to do is this:my $guy = Person->new({name => 'Bubba'}); print "name: ", $guy->get_name, "\n"; $guy->set_name('Gump'); print "name: ", $guy->get_name, "\n"; package Person; use Class::Std; { my %name_of :ATTR; sub BUILD { my ($self, $ident, $arg_ref) = @_; $name_of{$ident} = $arg_ref->{name}; return; } sub get_name { my $self = shift; return $name_of{ident $self}; } sub set_name { my ($self, $new_name) = @_; $name_of{ident $self} = $new_name; return; } }
But when I try I get the error message:my $guy = Person->new({name => 'Bubba'}); print "name: ", $guy->get_name, "\n"; $guy->set_name('Gump'); print "name: ", $guy->get_name, "\n"; package Person; use Class::Std; { my %name_of : ATTR(init_arg => 'name', get => 'name', set => 'name' +); }
I've also tried the perl6 syntax:Can't locate object method "get_name" via package "Person" at ./oop.pl + line 4
but I get the same error.:init_arg<name> :get<name> :set<name>
Obviously, I'm missing something really important here... can anyone please help me out?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Need help using Class:Std (with :ATTR)
by chromatic (Archbishop) on Feb 21, 2007 at 22:41 UTC | |
|
Re: Need help using Class:Std (with :ATTR)
by xdg (Monsignor) on Feb 22, 2007 at 15:07 UTC |