http://www.perlmonks.org?node_id=317621


in reply to Re: Re: Often Overlooked OO Programming Guidelines
in thread Often Overlooked OO Programming Guidelines

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
    • Comment on Re: Re: Re: Often Overlooked OO Programming Guidelines