- 论坛徽章:
- 0
|
http://www.garshol.priv.no/download/text/perl.html
其中一段
Not really object-oriented
Although object-orientation is not as fantastic as many would like us to think, Perl does support it. However, it does so only half-heartedly, since objects were added rather late in the life of the language.
The result of this is that normal files, sockets, hashes and lists are not objects, which means that the interfaces to them are not as convenient as they could have been. Newer versions of Perl come with object-oriented modules with wrappers for these kinds of objects, which means that Perl has a protocol for such objects and you can write your own implementations of these protocols. However, it also means that you need to distinguish between ordinary file handles and file objects, which is a bit inconvenient.
Another thing is the fact that when creating objects you need to explicitly manage the internals of your objects. In Perl, object creation is manual. A class is declared as a package, and the functions in the package then become the methods of the class. To create an object, you make a hash table and then bless it (using the built-in function 'bless') to make it an object. The 'perlobj' man page, which explains the Perl object features, recommends this form of object initializer:
package MyClass;
sub new {
my $class = shift;
my $self = {};
bless $self, $class
$self->initialize(); # do initialization here
return $self;
}
There are other ways of doing object initialization, some of which cause problems for inheritance. Personally, I find it amazing that this sort of thing should be necessary at all. The above is equivalent to this Python code:
class MyClass:
pass
The result is that one can easily get object construction wrong (such as by not catering for inheritance), defining classes is awkward and it's hard to tell from code when a class is defined (for both human readers and software documentation tools).
我個人比較相信那些言論,雖然我對PERL 理解不多,只知道它最初是作為 shell,sed,awk和Unix 眾多命令的
替代品和SuperSet, 它一開始便不是作為 OO語言開發的, Ruby 卻是作為OO語言開發的 scripting language |
|