Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
The stupid question is the question not asked
 
PerlMonks  

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:

Hi!

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.

Comment on Passing Module parameters to another module
Select or Download Code
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__ $ ./824136.pl Foo::import(): Foo x x(): bla
Re: Passing Module parameters to another module
by shmem (Canon) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://824136]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-04-21 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (490 votes), past polls