Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

A module to merge packages

by dakkar (Hermit)
on Jun 26, 2002 at 13:27 UTC ( [id://177377]=perlquestion: print w/replies, xml ) Need Help??

dakkar has asked for the wisdom of the Perl Monks concerning the following question:

I just wrote this little module. The problem it tries to solve is the following.

I have a set of packages, each of which specifies a part of the behaviour I need. The idea is to be able to exchange one specification with another (for those who know, I'm translating an ASM specification).

I wrote the subs in the packages to be used as methods, and one of the packages defines new, but this is not really relevant...

What I can do is something like:

package a; sub sa { return 1 } sub sb { return 2 } package b; sub sb { return 3 } package main; use Class::Merge qw(b a) => 'c'; $r= c->sb(); # returns 3

This, coupled with NEXT, gets me the result I need.

Now I ask:

  • Is this useful to anybody else?
  • Might it be worthy of CPAN?
  • I currently call it Class::Merge, but since it doesn't really involve classes, what could be a better name?

Oh, the whole module boils down to:

sub import { my $dest=pop; shift; eval <<"EOF"; package $dest; \@${dest}::ISA=qw(@_); EOF }

Replies are listed 'Best First'.
(tye)Re: A module to merge packages
by tye (Sage) on Jun 26, 2002 at 14:18 UTC

    Does your module boil down to more than the following?    @c::ISA= qw( b a ); If not, perhaps a more thorough description is in order.

            - tye (but my friends call me "Tye")

      Actually, no.

      It's just a fancy way to set that @ISA.

•Re: A module to merge packages
by merlyn (Sage) on Jun 26, 2002 at 15:37 UTC
    Ya know, instead of:
    use Class::Merge qw(b a) => 'c';
    I'd just as soon write:
    @C::ISA = qw(b a);
    as it is shorter and clearer, and doesn't require me to install a separate module. And it's "use strict" savvy. {grin}

    -- Randal L. Schwartz, Perl hacker

Re: A module to merge packages
by janx (Monk) on Jun 26, 2002 at 13:42 UTC
    Actually Schwern already has published something like that, but his version is based on randomness.

    You might call it Symbol::Merge.
    Or maybe even: Sub::Merge to keep in tradition with Simon Cozens' Sub::Versive.

    Kay

Re: A module to merge packages
by Baboon (Acolyte) on Jun 26, 2002 at 13:43 UTC
    There is already a module Sex which does what you do, and it's available as ActiveState installation package, and also exists on CPAN, version 0.69

    Best wishes,
    I.R.Baboon.

      I know of Schwern's Sex, but what the two modules do is different.

      My Merge creates a package whose @ISA is a list I specify.

      Sex creates a package by mixing up symbol table entries, both subs and variables, and (by the way) pushing the "parents" into the "child"'s @ISA.

      My first solution was a simplification of Sex (with less error checking, no schwernian messages, and no randomness), but it creates problem with classes, and makes it impossible to use NEXT to add functionality to a method, in something like:

      package a; sub method { my $self=shift; # do something } package b; use NEXT; sub method { my $self=shift; # do something before $self->NEXT::method(@_); # do the normal stuff # do something after } package main; require a;require b; use Merge qw(b a)=>'c'; $c=new c;$c->method; # will call "b"'s implemetation, which will call +"a"'s via NEXT

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://177377]
Approved by smitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found