in reply to Re^2: Skipping the middle man & the SUPER gotcha
in thread Skipping the middle man & the SUPER gotcha

It's useful when you have multiple-inheritance, to say which of your parent classes to start searching.

It's also useful when SUPER:: would be wrong... like a flyweight class.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on Re^3: Skipping the middle man & the SUPER gotcha

Replies are listed 'Best First'.
Re^4: Skipping the middle man & the SUPER gotcha
by tlm (Prior) on Apr 03, 2005 at 01:49 UTC

    It's useful when you have multiple-inheritance, to say which of your parent classes to start searching.

    Ah, of course, that make a lot of sense.

    It's also useful when SUPER:: would be wrong... like a flyweight class.

    I'm still scratching my head about this one. This is partly because I have not yet fully grokked Class::Prototyped (but I'm getting there :-) ), but partly because for the longest time I thougth that by "wrong" you meant bad OO design (and for the life of me I couldn't, and still can't, think of any scenario in which it would be OK design-wise to use $me->Dad::hello() but at the same time not OK to use $me->SUPER::hello()). Then it finally hit me that (maybe!) by "wrong" you meant "semantically wrong", as when the code in package main contains the expression $me->SUPER::hello() intending to access the hello method of $me's parent class. With "package-less classes" as those created with Class::Prototyped, there would not be any scope in which the SUPER pseudo-class had the desired meaning.

    So at least that explains (maybe) why SUPER is particularly problematic with classes/prototypes created with Class::Protoyped. But what I still can't understand is how the fact that Perl allows the $object->ParentClass::method() form can be of any help if such a class wanted to invoke a method in a parent class. For example (adapted from the Class::Prototyped docs):

    use strict; use Class::Prototyped ':EZACCESS'; my $Dad = Class::Prototyped->new( hello => sub { print "Hiya from $Dad!\n" } ); my $Me = Class::Prototyped::new( 'parent*' => $Dad hello => sub { # ??? print "I'm just a WILD AND CRAZY GUY!\n"; } );
    How can anything having the form $object->ParentClass::method() be used the definition of $Me's hello?

    the lowliest monk

      use strict; use Class::Prototyped ':EZACCESS'; my $Dad = Class::Prototyped->new( hello => sub { print "Hiya from $Dad!\n" } ); my $Me = Class::Prototyped::new( 'parent*' => $Dad hello => sub { # ??? print "I'm just a WILD AND CRAZY GUY!\n"; } );
      From further down in that same doc...
      'hello!' => sub { my $self = shift; my $mirror = $self->reflect; $mirror->super(@args_to_dad_hello); my code here; }
      The "hello!" here is the same as [qw(hello METHOD superable)]

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.