in reply to Re^2: Altering Package Subs and Running In To Problems
in thread Altering Package Subs and Running In To Problems

Even though the dispatch-ness sounds pretty cool, my goal is to have the base module have all the subs.

Understood. The advantage of using method dispatch over stash mangling is that you can have multiple instances which don't conflict.

package Foo; sub shared { print "Shared method" } sub foozle { print "Foo's foozle" } package Bar; sub foozle { print "Bar's foozle" } package Blip; sub foozle { print "Blip's foozle" } package FooBar; @ISA = qw( Bar Foo ); package FooBlip; @ISA = qw( Blip Foo );

With that kind of setup, you can choose which set of plugins you want to have active, and can have one part of your code use one plugin while another part does not.

package main; $package = 'Foo'; $package->shared(); $package->foozle(); # Calls Foo's foozle $package = 'FooBar'; $package->shared(); $package->foozle(); # Calls Bar's foozle

This approach also lets you dynamically stack your "plugins" so that a method in one of them can call NEXT and get the results of the next method up the chain; see my earlier article for details of this technique.

Replies are listed 'Best First'.
Re^4: Altering Package Subs and Running In To Problems
by Bovine (Initiate) on Nov 11, 2004 at 23:12 UTC

    Interesting, but not exactly what im looking for.

    Editing your code, I'd like something like this:
    (i get the multiple inheritance part, but the functionality is different, top-most subs are not accessed through the base module)

    package Foo; use Plugin 'Bar'; sub shared { print "Shared method" } sub foozle { print "Foo's foozle" } package Bar; our @PLUGIN_SUBS = qw(foozle); sub foozle { print "Bar's foozle" } # omitting Blip.. #package Blip; #sub foozle { print "Blip's foozle" } #======================= package main; $package = 'Foo'; $package->shared(); $package->foozle(); # Calls ***Bar's*** foozle
    From what i see theres no other way to have things work that way.

    Now this doesnt allow for having two instances of a plugin tree with the same base (at the moment), unfortunately.