... can we add new methods to a exist object in a package?
Update: WRT choroba's reply here, this reply addresses Perlish OO in general.
Yes, it's a new topic, but...
c:\@Work\Perl\monks>perl
use 5.014; # needs package NAME BLOCK syntax
use strict;
use warnings;
package Foo {
use strict;
use warnings;
sub new {
my $class = shift;
return bless { 'hi' => 'lo' } => $class;
}
sub method {
my $self = shift;
my ($k) = @_;
return exists $self->{$k} ? $self->{$k} : 'unknown';
}
}
my $of = Foo->new;
printf "A: '%s' \n", $of->method('hi');
printf "B: '%s' \n", $of->method('xx');
printf "C: '%s' \n", $of->oh_by_the_way('hi');
printf "D: '%s' \n", $of->oh_by_the_way('xx');
printf "E: '%s' \n", $of->method('hi');
sub Foo::oh_by_the_way {
my $self = shift;
my ($k) = @_;
$_ .= $_ for values %$self;
return exists $self->{$k} ? $self->{$k} : $self->method($k);
}
__END__
A: 'lo'
B: 'unknown'
C: 'lolo'
D: 'unknown'
E: 'lolololo'
The basic idea is that a subroutine in any package/namespace can be defined from within any other package/namespace if the newly-defined symbol is fully qualified.
Give a man a fish: <%-{-{-{-<
|