in reply to Is Perl Truly an Object Oriented Language?

I can't comment on 2), but as for 1) I would say no, definitely not.

When most people question the OO-ness of a supposedly OO language, usually it's because everything in the language is not an object. Alan Kay invented the term object-oriented to describe his language, Smalltalk, where everything is an object and all operations involve sending messages to objects.

Perl certainly doesn't fit Kay's definition, but even we use the more welcoming standard of the inheritince/encapsulation/polymorphism trio, I still think it doesn't pass, because Perl has no real object inheritance. Yes, you can inherit the methods from another class, but not objects. This results in cruft like this:

sub new { ... my $self = $class->SUPER::new; $self->{host} = shift; $self->{port} = shift; ... }

which clearly violates the law of encapsulation--as well as the law of abstraction--because you have to know the internals of the class you wish to inherit from, and then you go right on in and mess with it directly.

Replies are listed 'Best First'.
Re^2: Is Perl Truly an Object Oriented Language?
by Anonymous Monk on Dec 03, 2004 at 15:18 UTC

    I don't see how or why, unless ofc when you implement it wrong...

    First of all I don't really see a reason to call SUPER::new unless you are passing specific arguments which you should have specified (or unless there is initialisation which would better be done in a sub called initialise (or something like that)).

    Second, usually you pass arguments to the constructor as a hash reference, since this makes it easier to use and a lot easier to modify (since you don't need to know/care what arguments comes first).

    Third, if you need to know the internals of the class you are inheriting from then the implementation of that class isn't that good. If it would have been designed properly then it would have methods like set_host and/or set_port. (either normal methods or via the AUTOLOAD sub)

      You missed the point. "host" and "port" aren't the base class' data members, they're our data members. We created an object of the class we're inheriting from, then we went and messed with its internals by adding our own data members to it. See the problem?

      Here's an example (from WWW::Mechanize):

      my $self = $class->SUPER::new( %default_parms, @_ ); $self->{page_stack} = []; $self->{quiet} = 0; $self->env_proxy(); push( @{$self->requests_redirectable}, 'POST' ); return bless $self, $class;
Re^2: Is Perl Truly an Object Oriented Language?
by hardburn (Abbot) on Dec 03, 2004 at 15:57 UTC

    At some point, an inherited class has to know a little more about the internals of its parent than other code. This goes for any language, not just Perl. Not necessarily all the details, just more than normal.

    Further, I never consider data in the object's underlieing reference to be private. Rather, it's more analagous to Java's protected variables, which are explicitly allowed to be fiddled with by classes in the same namespace and/or subclasses. Java enforces this, though obviously Perl doesn't (without doing a lot of extra work, anyway).

    If the parent wants a truely private variable, it should have done:

    package Foo; my ($host, $port); # Private vars # rest of class

    Lexically scoped variables are very hard to get at outside the given lexical scope. At least as hard as a private var in Java.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      At some point, an inherited class has to know a little more about the internals of its parent than other code. This goes for any language, not just Perl. Not necessarily all the details, just more than normal.

      Not in Smalltalk. You inherit from classes all the time without knowing their internals; only what messages they respond to. In fact, *every* object inherits--at some point--from "Object", the root of the inheritance hierarchy. I honestly have no idea how Object's methods are implemented internally by the Smalltalk distro I'm using right now (Squeak), but I know what messages I can send to objects derived from it and how they'll respond to those messages, and that's good enough.

      It's not the fact that Perl doesn't prevent you from fiddling with the internals that rubs me the wrong way, it's the fact that you really can't do anything without fiddling.

      I'm not trying to bash Perl and I don't think OO is the be-all end-all, but this is one area where, unfortunately, Perl is exceptionally weak. But that's ok, because it's good at everything else :)

        If you don't want to fiddle don't. Declare accessor methods like in the other languages, allocate private dictionary (inside-out-design, or simply __PACKAGE__ or an allocationmethod in the base class TIMTOMTDI), and put your variables into that.
Re^2: Is Perl Truly an Object Oriented Language?
by BrentDax (Hermit) on Feb 05, 2005 at 23:21 UTC

    If that bothers you, just treat the object as a handle used to get at your real implementation:

    package My::Class; our @ISA=qw(Base::Class); my %obj; sub new { ... my $h=$class->SUPER::new; $obj{$h}{host}=shift; $obj{$h}{port}=shift; ... } sub DESTROY { my $h=shift; delete $obj{$h}; $h->SUPER::DESTROY; }

    Now Base::Class could change to using a tied filehandle as its implementation, for all you care.

    The reality is that most classes you would want to subclass are designed to make it safe, and even if they aren't there are a few simple measures you can take (like prefixing your subclass's keys with your module's name). And Perl is more than flexible enough for you to do things completely safely if you're really worried.

    =cut
    --Brent Dax
    There is no sig.