Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Usefulness of a SUPER-like pseudoclass: "DUPER"? Steve Roscio 01-09-2008
Posted by Steve Roscio on January 9, 2008, 4:29 pm
Please log in for more thread options
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 (@)
{
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.

Posted by Steve Roscio on January 10, 2008, 2:08 pm
Please log in for more thread options
By the way, would this be better as NEXT::EACH or SUPER::ALL or in some
other (suggested please) namespace?

Posted by Mumia W. on January 11, 2008, 6:22 am
Please log in for more thread options
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 (@)
> {
> 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.


Posted by Steve Roscio on January 11, 2008, 5:50 pm
Please log in for more thread options
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



Posted by Mumia W. on January 12, 2008, 12:18 pm
Please log in for more thread options
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.



Similar ThreadsPosted
Namespace and comments on usefulness sought October 26, 2004, 9:52 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap