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

I have a CGI::Application::Plugin that requires MyCgiApp to use another plugin.

Let's say CGI::Application::Plugin::Scotch makes use of CGI::Application::Plugin::Session.

How do I make it so MyCgiApp does not need to 'use CGI::Application::Plugin::Session;' ?

That is..

package MyCgiApp; use base 'CGI::Application'; use CGI::Application::Plugin::Scotch; # at this point, MyCgiApp should behave as if # CGI::Application::Plugin::Session was imported into the # package.

I can get similar behavior by importing CGI::Application::Plugin::Session into CGI::Application::Plugin::Scotch, and then exporting the methods I want out to the cgiapp.

package CGI::Application::Plugin::Scotch; use CGI::Application::Plugin::Session; use Exporter ... @EXPORT = qw/scotch session/;
But this seems messy. The only 'clean' solution seems to be..
package MyCgiApp; use base 'CGI::Application'; use CGI::Application::Plugin::Scotch; use CGI::Application::Plugin::Session;

Should I stick with that (seems clunky)- or is there some other way to get package x to import package y if you use package z?

Replies are listed 'Best First'.
Re: get package x to import package y if you use package z
by tilly (Archbishop) on Feb 18, 2009 at 17:59 UTC
    First tip. Get in the habit of using @EXPORT_OK rather than @EXPORT. You can thank me when you are debugging and are trying to find where scotch comes from.

    The usual way to solve this problem is to import and re-export. But that is what you don't want to do. So what else can we do?

    Well the underhanded way to do what you want is:

    use CGI::Application::Plugin::Session; sub import { my $package = "CGI::Application::Plugin::Session"; $_[0] = $package; goto $package->can("import"); }
    But that is kind of magic. And won't work if you want to do the re-export trick more than once. After peeking at Exporter carefully, you should be able to do the same thing as the above with the following:
    use CGI::Application::Plugin::Session; sub import { shift; # Throw away my class my $call_package = caller(); my $export_package = "CGI::Application::Plugin::Session"; Exporter::export($export_package, $call_package, @_); }
    And the advantage of this is that if you wanted to do this trick for multiple packages you could look in the @EXPORT and @EXPORT_OK lists for each, and call export on each while passing through the right arguments.

    Update: GrandFather noted a missing ')'. Fixed.

Re: get package x to import package y if you use package z
by JavaFan (Canon) on Feb 18, 2009 at 16:59 UTC
    Well, you always export everything you want from package Foo into package Bar, without every having to write code into either Foo or Bar. After all, an "export" of a token is nothing more, and nothing less than a single assignment of a typeglob slot.

    Having said that, what I find the cleanest solution is that if package X needs package Y, that then package X uses Y itself instead of some other package providing that for it. Less action on a distance.