in reply to Re: Often Overlooked OO Programming Guidelines in thread Often Overlooked OO Programming Guidelines
These are some interesting points you made. What stands out to me in your comments is that you've further shown (whether you intended it or not) how Perl's OO, while useful, is a hack and has some serious limitations. I've been working on an piece which shows Perl's limitations and uses Java's OO style as an example (because I know Java better than other OO languages, not because Java is the pinnacle of OO). I think some of your points will fit in quite nicely there.
Re: Re: Re: Often Overlooked OO Programming Guidelines
by scrottie (Scribe) on Dec 30, 2003 at 03:49 UTC
|
Hi again Ovid,
I've gone on personal missions to "fix" Perl's hackish
OO. Object::Lexical is one attempt - instance data
is held in lexicals and accessors/mutators are
closures, created with Sub::Lexical.
To quote the POD:
<code>
use Object::Lexical;
use Sub::Lexical;
sub new {
my $counter;
our $this;
my sub inc {
$counter++;
}
my sub dec {
$counter--;
}
my sub inc3x {
$this->inc() for(1..3);
}
instance();
}
This skirts a few issues, which are issues unique to
Perl:
- Different syntax for instance data than other variables
(my, local) makes refactoring code hard - every "$foo" must be changed to "$self->{foo}" manually.
- No help from "use strict" and "use warnings" about
only using a name once. None of the protection of requiring
that a variable be declared first. In short, hash entries
don't get all of the diagnostic helpfullness perl gives
lexicals.
- CPU overhead for hash lookups, memory overhead for
storing hashes.
Each object is given its own stash (namespace) which
inherits from the namespace of the current package.
The stash is populated with closures. Viola!
Thanks to Juerd, by the way, who suggested
creating things and stuffing them into stashes instead
of sticking an AUTOLOAD to proxy to methods stored in
hashes.
Then there is typesafety:
http://search.cpan.org/~swalters/typesafety-0.04/
with its massive userbase of 0 users. This cultural
divide miffs me - no Java user would ever consider
a language that didn't have typesafety, and no Perl
programmer would ever willingly use typesafety.
Actually, I know (or know of atleast) a lot of people that
lost their Perl jobs and had to get Java jobs, and now
would never go back to Perl because of things like the
lack of typesafety and other OO hackishness in Perl,
which just goes to show, people don't know what's
good for them.
-scott
| [reply] [Watch: Dir/Any] |
Re: Re: Re: Often Overlooked OO Programming Guidelines
by ysth (Canon) on Dec 30, 2003 at 06:02 UTC
|
I disagree with the categorization of Perl's OO as a hack. I think it provides the minimal tools as part of the core language, and gives lots of room for addition, restriction, expansion, and factorization. This is evidenced by the wide variety of modules to change/enhance/replace/invert/etc. perl's core object model.
Whether it is good to have such a plethora of ways and means (that you have to go looking for) instead of a Single True Way To Do It, Blessed By The Core, is a separate discussion.
Update: Yes, Perl OO is constrained, and yes, it was grafted onto an existing language; I don't agree that the constraints are a consequence of the grafting. I think they are an intentional design decision (though from recent evidence, Larry wouldn't make the same decisions now).
Update: Seems we just disagree on what hack means. To me it means poor features based on external constraints (whether that be time, backward compatibility, grafting new features onto old code, or whatever).
I think the limitations of Perl OO due to intentional design, not constraint, so I would call it minimalist rather than hacked.
Update: Perl has no useful subroutine prototypes even for non-methods, by the definition of prototypes in other popular languages. The purpose of Perl prototypes (user subs that work like builtins) doesn't apply to object methods.
Update: It's strange to see multiple inheritance cited as a reason to call Perl OO hackish. It's the one feature that isn't what I would call minimalist. It's also hard to see how you could accidentally use it. It's not my fault, Officer, the language made me use multiple inheritance. Click go the closing handcuffs. | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
I disagree with the categorization of Perl's OO as a hack.
I'd classify Perl's OO as a hack: But that doesn't mean it isn't a
useful, flexible, and powerful hack. But it *is* hacked / grafted on
to an existing language, and the imposed constraints shine through the
finished product.
| [reply] [Watch: Dir/Any] |
|
Hi ysth,
"Hackish" is kind of vague, perhaps intentionally.
I don't think anyone is saying perl's OO is bad, just
quick and dirty. The implications of what it is are
more interesting than what it is. Implications:
- Not default. Programs aren't forced to use it. Good,
in my book.
- Not helpful.
- Doesn't encourage good style.
Negitives, point by point:
Not Helpful:: That is, completely unhelpful in diagnostics
and debugging compared with virtually any other OO implementation. Privacy levels, prototypes/typechecking,
and so on exist to help a programmer who can't keep
a large project in his head (or a team of programmers
who can't). If a method doesn't exist I'm trying to
call, I want to know about it at compile time, not
a month down the road when a client clicks on things
in the wrong order and some corner case runs.
Doesn't encourage good style:: Java is adding multiple
inheritance grudgingly, and a lot of people say they
shouldn't. It has willfully not so far. So, Java makes
good style easy and bad style possible. Perl makes
bad style easy and good style possible. Privacy,
finalization, data hiding, strict typechecking
(yes, you heard me) and so on are all possible in Perl,
but they aren't easy. They require additional syntax
and sometimes don't mesh well with other language features.
On the other hand, breaking encapsulation, multiple
inheritance, and other things of questionable style
are all easy in Perl. This doesn't matter if you know
your way around, but a novice programmer assumes that
is easy is what they are supposed to be doing. If the
langauge encourages you to multiple inherit and encourages
you to access data in a class as if it were a hash,
then it must be the best way. People don't read
documentation until and unless all other things fail.
This is a well known principle =)
So, when I say Perl's OO is hackish, what I personally
mean is it is something that kind of happened through
tinkering and building (hacking), not something designed,
specified, tested on focus groups, with second opinions
from industry experts. But that doesn't mean that we
can't continue to hack on it in future versions (eg, 6)
and do RFC processes to form a coherent design,
learn from feedback, visiting Java programmers, and
so on. Hackish isn't fundamentally bad. It fits with
"release early, release often". We just need to hack
on it some more so it is less hackish =)
-scott
| [reply] [Watch: Dir/Any] |
|
If a method doesn't exist I'm trying to call, I want to know about it at compile time
You aren't going to get missing-method detection at compile time in a dynamic
language, so that won't qualify in my book as part of Perl's OO
'hackishness' (I guess it can in your book, but then you'd also need to
find Ruby and Smalltalk's OO hackish as well).
| [reply] [Watch: Dir/Any] |
|
|