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

I've a problem and I'm not sure where to start.

I've got 2 pacakges Foo.pm and Bar.pm I'd like package Foo to be able to intercept all calls to package Bar, perform some actions and then forward the call to package Bar. Basically ot the end user it will apear package Foo is doing everything...they don't even know package Bar exists. Another twist is that package Bar may not exist or be available so it will have to be loaded on the fly.

I've been mucking with AUTOLOAD but I can't semm to figure it out. I don't even know if AUTOLOAD is what I should be using.

Any ideas?? Thanks a whole lot in advance. Save my bacon if you can help.

Foo::Bar

Replies are listed 'Best First'.
Re: Intercept Call to another package
by dragonchild (Archbishop) on Nov 29, 2001 at 00:17 UTC
    Ummm... what's wrong with having Foo inherit from Bar through @ISA? This would mean that the users of Foo would have to understand how to use OO, but that's not that difficult, and it's even a little more intuitive, in many cases.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      So...Foo could override the method in Bar and then call Foo::SUPER->method ? That would work but what if I want to do the same with modules in the future I know nothing about, sort of like a plugin? Maybe there is a better way or my thinking is all wrong. I dont' want to always be updating pacakge Foo. I want Foo to dynamically load Bar, Baz..others (from a config file), basically override the methods for the intercept and then call the method. Also, if I use plain old ISA I could have conflictig method names: Bar->method1, Bax->method1.

      Maybe I'm way of base here...seems like I should be able to do it though.
        Yes, Foo could override the method, then call $self->SUPER::method. (Slight difference, same idea.)

        Now, if you're talking about doing some sort of plugin idea ... that's getting really complicated, involving on-the-fly modifications to @ISA, etc. Essentially, what it sounds that you really want to do is create a dispatch table that would have, instead of a coderef, a list of coderefs (or function names) that would be called in that order.

        There has to be a better way to do what you want to do.

        Period.

        Maybe if you started describing what it is you want to design for, we can help.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Intercept Call to another package
by runrig (Abbot) on Nov 29, 2001 at 01:32 UTC
    Keeping in mind that this sounds truly evil, though it does sound a bit like Damian's Hook::LexWrap (though I don't think that redefines any subs, just defines new methods with pre and post processing, but you may want to look at that instead/anyway), and you're better off using dragonchild's suggestions of utilizing ISA and SUPER in an orthodox manner if at all possible:
    use strict; use warnings; package Bar; sub f1 { print "there @_\n"; } package Foo; for (keys %Bar::) { if (exists &{$Bar::{$_}}) { no warnings 'redefine'; my $func = \&{$Bar::{$_}}; $Bar::{$_} = sub { print "hello "; goto &$func; }; } } package main; Bar::f1("Bob");
Re: Intercept Call to another package
by grinder (Bishop) on Nov 29, 2001 at 18:48 UTC