Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Passing Module parameters to another module

by voj (Acolyte)
on Feb 19, 2010 at 11:31 UTC ( #824136=perlquestion: print w/replies, xml ) Need Help??
voj has asked for the wisdom of the Perl Monks concerning the following question:


I want to pass module parameters given to a module 'Foo' to be passed to another module 'Bar'. 'Bar' is not a subclass of 'Foo'. For instance if import the Module with

use Foo ('x');
The parameter 'x' should be passed to 'Bar' inside of 'Foo':
package Foo; use Bar ('x'); # this 'x' should not be hard-coded but set dynamically + depending on what is passed to 'Foo'. 1;
I fiddled around with Exporter but could not find a right way.

Replies are listed 'Best First'.
Re: Passing Module parameters to another module
by almut (Canon) on Feb 19, 2010 at 12:11 UTC

    Maybe something like this (if I'm understanding you correctly):

    package Bar; use Exporter 'import'; our @EXPORT_OK = qw(x); sub x { print "x(): @_\n"; } 1;
    package Foo; sub import { print "Foo::import(): @_\n"; # debug require Bar; # do whatever you want with the import() args, e.g. shift @_; Bar->import(@_); } 1;
    #!/usr/bin/perl use Foo 'x'; # x() will be imported from Bar into Foo namespace # (as would be done when using a hardcoded "use Bar 'x' +" within Foo) Foo::x('bla'); __END__ $ ./ Foo::import(): Foo x x(): bla
Re: Passing Module parameters to another module
by shmem (Chancellor) on Feb 20, 2010 at 12:40 UTC

    Do a require and import for package Bar:

    package Foo; require Bar; sub import { my $class = shift; # first param is class name if (@_) { Bar->import(shift); # that's passed along # with 'use Foo qw(x);' } } # more stuff... 1;
    package Bar; sub import { my $class = shift; if (@_) { my $x = shift; # passed from Foo warn "$class: got '$x' from ".caller()."\n"; # now do whatever you like with 'x' } } # more stuff... 1;

    and then...

    #!/usr/bin/perl use Foo qw(blorf);

    ...or even

    $ perl -MFoo=blorflydick -e 1 Bar: got 'blorflydick' from Foo

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://824136]
Approved by AnomalousMonk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-06-22 15:29 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (523 votes). Check out past polls.