V
Veli-Pekka Tätilä
Hi,
I'm new to OOP in Perl having only used classes by others, read some book's
take on the subject and implemented closures for quick-n-dirty object-like
thingies. One thing I didn't see mentioned in Beginning PErl is Perl's take
on what Java calls interfaces and C++ programmers pure virtual abstract base
classes. That is in stead of a real class with state or useful methods, it
is something non-instanciable - a contract that concrete sub-classes will
have a certain set of methods in them. Is there a good way to implement an
interface in Perl or some perlish idiom for achieving the same thing?
I've noticed that most Perl classes don't rely too much on abstraction where
as in Java I'm almost always talking through some sort of interface. Don't
get me wrong, though, usually I like Perl's simple and practical ways of
achieving what you want, <grin>.
Also, if there's such a thing as an interface in Perl, how is it used and
what about type safety? In Java one would get a reference to the interface
and can assign to it any class implementing the interface. Java will then
automagically see that the right method gets called inside the class and we
have polymorphism. Further more, type checking is done at compile time for
the most part.
From what I've understood objects are actually blessed references in Perl
and when assigning to a reference, the type of the referent may vary freely.
Thus the best you can do is to keep assigning the right sub-classes derived
from a dummy interface class and hope that you don't make any mistakes in
the process, right? The only trouble with that is accidentally assigning
something that does not conform to the interface . Perl will only throw an
error at compile time should the missing method or methods get called. I've
noticed that even in trivial cases, typoed method names are not catched when
checking the syntax. PHP 4 does come to mind, argh, but fortunately Perl's
OOP stuff is not at all as toy-like as it used to be in PHP.
In contrast, should one try using operators, functions or references with
the wrong primitive type e.g. hash functions for arrays, you'll usually get
at least a compile-time error before the app is run. is there any way of
enforcing similar type safety for user-created objects?
I suppose you could use reflection to some effect with the methods in the
universal base class. Checking whether the referent is derived from the
interface by calling isa or asking if it does support a particular method
with the appropriately named method can.
Is there a better way than reflection i.e. relying on prototypes or being
able to strongly type references?
Lastly, speaking of Perl's OOP stuff, I remember a funny quote about modules
from the Camel book:
<cite>
Perl does not patrol private/public borders within its modules - unlike
languages such as C++, Ada, and Modula-17, Perl isn't infatuated with
enforced privacy. As we mentioned at the beginning of the chapter, a Perl
module would prefer that you stayed out of its living room because you
weren't invited, not because it has a shotgun.
</cite>
I'm new to OOP in Perl having only used classes by others, read some book's
take on the subject and implemented closures for quick-n-dirty object-like
thingies. One thing I didn't see mentioned in Beginning PErl is Perl's take
on what Java calls interfaces and C++ programmers pure virtual abstract base
classes. That is in stead of a real class with state or useful methods, it
is something non-instanciable - a contract that concrete sub-classes will
have a certain set of methods in them. Is there a good way to implement an
interface in Perl or some perlish idiom for achieving the same thing?
I've noticed that most Perl classes don't rely too much on abstraction where
as in Java I'm almost always talking through some sort of interface. Don't
get me wrong, though, usually I like Perl's simple and practical ways of
achieving what you want, <grin>.
Also, if there's such a thing as an interface in Perl, how is it used and
what about type safety? In Java one would get a reference to the interface
and can assign to it any class implementing the interface. Java will then
automagically see that the right method gets called inside the class and we
have polymorphism. Further more, type checking is done at compile time for
the most part.
From what I've understood objects are actually blessed references in Perl
and when assigning to a reference, the type of the referent may vary freely.
Thus the best you can do is to keep assigning the right sub-classes derived
from a dummy interface class and hope that you don't make any mistakes in
the process, right? The only trouble with that is accidentally assigning
something that does not conform to the interface . Perl will only throw an
error at compile time should the missing method or methods get called. I've
noticed that even in trivial cases, typoed method names are not catched when
checking the syntax. PHP 4 does come to mind, argh, but fortunately Perl's
OOP stuff is not at all as toy-like as it used to be in PHP.
In contrast, should one try using operators, functions or references with
the wrong primitive type e.g. hash functions for arrays, you'll usually get
at least a compile-time error before the app is run. is there any way of
enforcing similar type safety for user-created objects?
I suppose you could use reflection to some effect with the methods in the
universal base class. Checking whether the referent is derived from the
interface by calling isa or asking if it does support a particular method
with the appropriately named method can.
Is there a better way than reflection i.e. relying on prototypes or being
able to strongly type references?
Lastly, speaking of Perl's OOP stuff, I remember a funny quote about modules
from the Camel book:
<cite>
Perl does not patrol private/public borders within its modules - unlike
languages such as C++, Ada, and Modula-17, Perl isn't infatuated with
enforced privacy. As we mentioned at the beginning of the chapter, a Perl
module would prefer that you stayed out of its living room because you
weren't invited, not because it has a shotgun.
</cite>