[Mpls-pm] Dumb Question...Module Method - Dual Name
Ian Malpass
ian at indecorous.com
Mon Oct 24 08:59:00 PDT 2005
On Sun, 23 Oct 2005, Gary Vollink wrote:
> O.K., I think I finally have an idea of what is going on, and I'd like
> to thank each of you for introducing various ways to get it done.
Ah, perl. Bless your multiple cat-skinning methodologies.
> I definately need to read more about the differences between 'Instance
> method', 'class method' and 'function'... but for now, I understand
> enough to get what I need, done...
# Class method
Example->foo( 'bar' )
# Example::foo gets 'Example', 'bar' as its arguments.
# Instance/object method
$eg = Example->new();
$eg->foo( 'bar' );
# Example::foo gets (reference to) $eg, 'bar' as its arguments.
# Subroutine (probably equivalent to function, really)
Example::foo( 'bar' );
# Example::foo gets 'bar' as its argument.
So the difference between an instance and a class method is that the
former is called on a blessed reference (object), and gets a copy of the
object in its arguments, and so the method has access to other data stored
in the object. The class method gets the class name as a string as its
first argument instead[0].
A class method is then just a method that doesn't try to access any object
data. You can call a class method on an object quite happily, but not vice
versa. (As far as perl is concerned, methods are methods - there is no
explicit differentation between the two, merely practical
differentiation.)
Read perldoc perltoot, and then read Object Oriented Perl by Damian
Conway[1]. Or skip perltoot and just go to OOP. It's very good.
> So I understand OO theory, I've just never had to deal with it in a
> day-to-day basis. Yet, if I want to become the all-powerful Perl Guru
> than I know I can be... I'm going to have to get a lot more practical
> experience with it. :-)
The major problem with/strength of OOP is that it's rather lax OO
(especially in terms of encapsulation, although Conway discusses ways to
enforce some encapsulation in his book). Many OO purists disparage perl's
OO. Many OOPists laud its laid back approach. If you want to learn or
apply OO theory, perl might not be the best language to choose.
Speaking as someone with no formal CS training, I like OOP :)
Ian
P.S. I didn't mention it in my mail about AUTOLOADing, but one thing I
often do to try to make sure I don't do anything untoward is have some
sort of list of allowed methods, and have AUTOLOAD complain if it's not
one of those. You could probably leverage @EXPORT for that purpose.
[0] A common way to do object creation is
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
return bless {}, $class;
}
Thus you can do $eg->new() as well as Example->new(). If you call new() as
an object method, $proto is the object, and calling ref $proto will give
you its class name. If you call it as a class method, $proto is the class
name, and ref $proto is undef (and false), so you get the value of $proto
instead. I don't know that I've ever created an object using an object
method, though.
[1] <http://www.amazon.com/gp/product/1884777791>
-
---------------------------------------------------------------------------
The soul would have no rainbows if the eyes held no tears.
Ian Malpass
<ian at indecorous.com>
More information about the Mpls-pm
mailing list