R
Rainer Weikusat
That's something which probably also got lost in the noise: A
subroutine which expects 'an object' (a blessed reference) as argument
and does nothing with this object except invoking methods on it will
work with any object whose corresponding class ('package it is blessed
into _at the moment_') enables access to suitably name subroutines.
Example
-------------
package Named;
use Scalar::Util qw(blessed);
sub name
{
return blessed($_[0]);
}
package Brazilian::Jaguar;
our @ISA = 'Named';
sub new
{
return bless([], $_[0]);
}
sub what
{
return 'feline predator';
}
package British::Jaguar;
our @ISA = 'Named';
sub new
{
my $x;
return bless(\$x, $_[0]);
}
sub what
{
return 'car';
}
package main;
sub info
{
printf("The %s is a %s\n.", $_[0]->name(), $_[0]->what());
}
my ($j0, $j1);
$j0 = Brazilian::Jaguar->new();
$j1 = British::Jaguar->new();
info($j0);
info($j1);
--------------
Considering that such a subroutine is totally independent of any
package providing object methods, it should not be tied to one because
the code could be useful for all kinds of objects. Such a subroutine
really belongs into a general, non-OO subroutine library.
NB: The obvious implication is that no 'method' which only expects to
work with a 'thing' providing access to a certain set of 'named
properties' actually belongs to the 'the class proper'.
subroutine which expects 'an object' (a blessed reference) as argument
and does nothing with this object except invoking methods on it will
work with any object whose corresponding class ('package it is blessed
into _at the moment_') enables access to suitably name subroutines.
Example
-------------
package Named;
use Scalar::Util qw(blessed);
sub name
{
return blessed($_[0]);
}
package Brazilian::Jaguar;
our @ISA = 'Named';
sub new
{
return bless([], $_[0]);
}
sub what
{
return 'feline predator';
}
package British::Jaguar;
our @ISA = 'Named';
sub new
{
my $x;
return bless(\$x, $_[0]);
}
sub what
{
return 'car';
}
package main;
sub info
{
printf("The %s is a %s\n.", $_[0]->name(), $_[0]->what());
}
my ($j0, $j1);
$j0 = Brazilian::Jaguar->new();
$j1 = British::Jaguar->new();
info($j0);
info($j1);
--------------
Considering that such a subroutine is totally independent of any
package providing object methods, it should not be tied to one because
the code could be useful for all kinds of objects. Such a subroutine
really belongs into a general, non-OO subroutine library.
NB: The obvious implication is that no 'method' which only expects to
work with a 'thing' providing access to a certain set of 'named
properties' actually belongs to the 'the class proper'.