Usefulness of a SUPER-like pseudoclass: "DUPER"?

Usefulness of a SUPER-like pseudoclass: "DUPER"?

am 09.01.2008 22:29:23 von Steve Roscio

Howdy -

Is this something worth posting to CPAN? See the POD about two dozen
lines down. It's like a simpler, faster but dumber flavor of NEXT.

Thanx,
- Steve

package DUPER;
use strict;
use warnings;
our $VERSION = '0.07';
use vars qw($AUTOLOAD);

sub AUTOLOAD
{
my $func = $AUTOLOAD; $func =~ s/^DUPER:://o;
my $base = shift;
my $name = (caller(1))[3]; $name =~ s/^(.+?)::\w+$/$1/io;
no strict 'refs';
my $ret = undef;
foreach (@{"$name\::ISA"})
{
my $f = $_."::$func";
$ret = $base->$f(@_);
}
return $ret;
}

1;

__END__

=head1 NAME

DUPER.pm - Works like SUPER but dispatches to each parent.

=head1 SYNOPSIS

use DUPER;
$this->SUPER::MyFunc(@_); # Only dispatches to first @ISA parent
$this->DUPER::MyFunc(@_); # Dispatches to *all* @ISA parents

=head1 DESCRIPTION

The DUPER pseudo-class works like the SUPER pseudo-class, but
instead of dispatching to the first of the @ISA parent classes,
it dispatches to each of the @ISA parent classes.

DUPER only useful if your class does multiple inheritance --
if you have more than one thing in your @ISA array.
Otherwise you should just use SUPER.

This class was designed to be small and fast. As such,
it doesn't do much! It DOES NOT handle the diamond-inheritance
situation, so if that is a problem for your class, then you
probably want to use NEXT instead of DUPER. In fact, NEXT
is a very good class that you probably should be using anyway,
except that NEXT is a bit slow.

But if you don't care about the diamond problem,
then DUPER is just fine.

=head2 Return Value

What about the return value from the call -- which
value is returned? Just the last one.

=head2 HUH?

Why the name DUPER? The name has nothing to do with
duplication; I chose it because of the closeness to the SUPER
name, and it's super-duper!

If that's just too stupid a name, then I'll consider renaming
this class to "EACH", since it calls *each* member of @ISA.
Lemme know.

=head1 COPYRIGHT

Copyright (c)2007 by Steve Roscio. All rights reserved.
This module is free software. It may be used, redistributed
and/or modified under the same terms as Perl itself.

Re: Usefulness of a SUPER-like pseudoclass: "DUPER"?

am 10.01.2008 20:08:32 von Steve Roscio

By the way, would this be better as NEXT::EACH or SUPER::ALL or in some
other (suggested please) namespace?

Re: Usefulness of a SUPER-like pseudoclass: "DUPER"?

am 11.01.2008 12:22:50 von paduille.4061.mumia.w+nospam

On 01/09/2008 03:29 PM, Steve Roscio wrote:
> Howdy -
>
> Is this something worth posting to CPAN? See the POD about two dozen
> lines down. It's like a simpler, faster but dumber flavor of NEXT.
>
> Thanx,
> - Steve
>
> package DUPER;
> use strict;
> use warnings;
> our $VERSION = '0.07';
> use vars qw($AUTOLOAD);
>
> sub AUTOLOAD
> {
> my $func = $AUTOLOAD; $func =~ s/^DUPER:://o;
> my $base = shift;
> my $name = (caller(1))[3]; $name =~ s/^(.+?)::\w+$/$1/io;
> no strict 'refs';
> my $ret = undef;
> foreach (@{"$name\::ISA"})
> {
> my $f = $_."::$func";
> $ret = $base->$f(@_);
> }
> return $ret;
> }
>
> 1;
>
> __END__
>
> =head1 NAME
>
> DUPER.pm - Works like SUPER but dispatches to each parent.
>
> =head1 SYNOPSIS
>
> use DUPER;
> $this->SUPER::MyFunc(@_); # Only dispatches to first @ISA parent
> $this->DUPER::MyFunc(@_); # Dispatches to *all* @ISA parents
>
> =head1 DESCRIPTION
>
> The DUPER pseudo-class works like the SUPER pseudo-class, but
> instead of dispatching to the first of the @ISA parent classes,
> it dispatches to each of the @ISA parent classes.
>
> DUPER only useful if your class does multiple inheritance --
> if you have more than one thing in your @ISA array.
> Otherwise you should just use SUPER.
>
> This class was designed to be small and fast. As such,
> it doesn't do much! It DOES NOT handle the diamond-inheritance
> situation, so if that is a problem for your class, then you
> probably want to use NEXT instead of DUPER. In fact, NEXT
> is a very good class that you probably should be using anyway,
> except that NEXT is a bit slow.
>
> But if you don't care about the diamond problem,
> then DUPER is just fine.
>
> =head2 Return Value
>
> What about the return value from the call -- which
> value is returned? Just the last one.
>
> =head2 HUH?
>
> Why the name DUPER? The name has nothing to do with
> duplication; I chose it because of the closeness to the SUPER
> name, and it's super-duper!
>
> If that's just too stupid a name, then I'll consider renaming
> this class to "EACH", since it calls *each* member of @ISA.
> Lemme know.
>
> =head1 COPYRIGHT
>
> Copyright (c)2007 by Steve Roscio. All rights reserved.
> This module is free software. It may be used, redistributed
> and/or modified under the same terms as Perl itself.

This is potentially useful, but what happens if someone attempts to
invoke methods that do not exist?

$this->DUPER::nowhere();

$this->another_nonmethod();

I think DUPER is better than EACH, but try to find a better package
namespace such as Class::DUPER.

Re: Usefulness of a SUPER-like pseudoclass: "DUPER"?

am 11.01.2008 23:50:29 von Steve Roscio

Mumia W. wrote:
> This is potentially useful, but what happens if someone attempts to
> invoke methods that do not exist?

It bombs. My original use favored speed over completeness, but
this should be fixed. i'll address it.


> I think DUPER is better than EACH, but try to find a better package
> namespace such as Class::DUPER.

What do you think of SUPER::ALL, SUPER::EACH (EVERY), or something along
those lines? I could also try to stick it in NEXT/EVERY as EVERY::FAST.
DUPER is essentially a fast version of EVERY::LAST. Naming
suggestions welcome.

Too, I'll spend some time to see if I can speed up EVERY::* instead of
submitting a whole new module. But EVERY::* already looks pretty tight
so I don't think I'll be very successful there.

- Steve

Re: Usefulness of a SUPER-like pseudoclass: "DUPER"?

am 12.01.2008 18:18:00 von paduille.4061.mumia.w+nospam

On 01/11/2008 04:50 PM, Steve Roscio wrote:
> Mumia W. wrote:
>> This is potentially useful, but what happens if someone attempts to
>> invoke methods that do not exist?
>
> It bombs. My original use favored speed over completeness, but
> this should be fixed. i'll address it.
>
>
>> I think DUPER is better than EACH, but try to find a better package
>> namespace such as Class::DUPER.
>
> What do you think of SUPER::ALL, SUPER::EACH (EVERY), or something along
> those lines? I could also try to stick it in NEXT/EVERY as EVERY::FAST.
> DUPER is essentially a fast version of EVERY::LAST. Naming suggestions
> welcome.
> [...]

Don't create a new top-level namespace. The Class:: namespace already
exists and placing the module under it will give people a hint of what
the module does.