I'm trying to get a handle on Class::Multimethods but I can't seem to get it to do basic class accessor methods when if nothing is passed to the method it just returns the class field for the method, otherwise if a string is passed, it assigned that string to the field. For instance:
I know that this is easily possible doing basic Perl, but I'm hoping to get into fancier things after I figure out how this works.my $f = Flub->new("Cheese is Good."); print $f->flubble() . "\n"; $f->flubble("Cheese is Bad"); print $f->flubble() . "\n";
My full code is below.
Thanks!
#!/usr/bin/perl -w package Flub; use strict; use Class::Multimethods; sub new { my ($class) = @_; bless { _flub => $_[1] || "", }, $class; } # new multimethod flubble => ('*') => sub { my ($self) = shift; return $self->{_flub}; }; multimethod flubble => ('$') => sub { my ($self) = shift; $self->{_flub} = shift; return $self->{_flub}; }; package main; use strict; my $f = Flub->new("Cheese is Good."); print "Trying to display \$f->{_flub}: " . $f->flubble() . "\n"; print "Trying to assign Cheese is Bad to \$f->{_flub}: " . $f->flubble("Cheese is Bad ") . "\n";
In reply to Basic Accessor Methods using Class::Multimethods by ignatz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |