I'm trying to inherit and over-ride a package's functions. What I've come up with is this (for demonstration purposes):
The original package
package orig::functions; BEGIN { use Exporter (); use vars qw(@ISA @EXPORT_OK); @ISA = qw(Exporter); @EXPORT_OK = qw(func1 func2 func3); } sub func1 { print "hello"; } sub func2 { print "kind"; } sub func3 { print "world"; }
The replacement package. Inherits original functions, replaces some and adds new ones
package replace::functions; BEGIN { # import the original functions, but not the ones # this package is going to replace use orig::functions qw(!/func1|func2/); use Exporter (); use vars qw(@ISA @EXPORT_OK); @ISA = qw(Exporter); # let the original functions be exported as well # as the new ones @EXPORT_OK = (@orig::functions::EXPORT_OK, qw(funcx funcy) ); } # func1 is going to be replaced with our own sub func1 { print "goodbye"; } # func2 is going to be replaced with our own sub func2 { orig::functions::func2(); print "(haha)"; } # func3 is reused from orig::functions unchanged # a couple new functions sub funcx { print "new x"; } sub funcy { print "new y"; }
Now to pull it all together...
and# program A use orig::functions qw(func1 func2 func3); print func1(), func2(), func3(), "\n";
# program B use replace::functions qw(func1 func2 func3 funcx); print func1(), func2(), func3(), "\n"; print funcx(), "\n";
My goal is to use orig::functions as common base of shared functions. But I'd also like to modify the shared functions as required with replace::functions. This layout works as intended it, but it feels like I'm skulking around corners to achieve it. Or maybe I'm just looking at the problem the wrong way. Is there a cleaner way to get the same results without using blessed objects?
In reply to Sharing and over-riding package functions by ruzam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |