perl require

B

bob

Is it possible to dynamically change the location of a module
i.e.
require Plugins::Wetherbys::Generate::GenerateXML;
Plugins::Wetherbys::Generate::GenerateXML->import('help');

works fine

but ideally

i would like

require Plugins::$VAR::Generate::GenerateXML;
Plugins::Wetherbys::$VAR::GenerateXML->import('help');

I have looked through the FAQs and couldn't find anything.
I know you can do require $VAR;

Cheers
 
A

Anno Siegel

bob said:
Is it possible to dynamically change the location of a module
i.e.
require Plugins::Wetherbys::Generate::GenerateXML;
Plugins::Wetherbys::Generate::GenerateXML->import('help');

works fine

but ideally

i would like

require Plugins::$VAR::Generate::GenerateXML;
Plugins::Wetherbys::$VAR::GenerateXML->import('help');

Your original example has the same string twice. These two lines
can never be the same string, no matter what $VAR contains. I'll
assume you meant

require Plugins::$VAR::Generate::GenerateXML;
Plugins::$VAR::Generate::GenerateXML->import( 'help');
I have looked through the FAQs and couldn't find anything.
I know you can do require $VAR;

You can even do "require EXPRESSION", but the problem is that "require"
in that form expects a file name and the method call needs a class name.
You won't be able to do this with a single variable, but this should work
(untested):

my $VAR = 'Wetherbys';
my $class = "Plugins::$VAR::Generate::GenerateXML";
( my $module = $class) =~ s{::}{/}g; # File::Spec would be better
$module .= '.pm'; # File::Spec would be better
require $module;
$class->import('help');

This mess may be best stuffed into a sub and taken out of sight.

Anno
 
B

bob

cheers ill give it a try.
Anno Siegel said:
Your original example has the same string twice. These two lines
can never be the same string, no matter what $VAR contains. I'll
assume you meant

require Plugins::$VAR::Generate::GenerateXML;
Plugins::$VAR::Generate::GenerateXML->import( 'help');


You can even do "require EXPRESSION", but the problem is that "require"
in that form expects a file name and the method call needs a class name.
You won't be able to do this with a single variable, but this should work
(untested):

my $VAR = 'Wetherbys';
my $class = "Plugins::$VAR::Generate::GenerateXML";
( my $module = $class) =~ s{::}{/}g; # File::Spec would be better
$module .= '.pm'; # File::Spec would be better
require $module;
$class->import('help');

This mess may be best stuffed into a sub and taken out of sight.

Anno
 
G

GreenLight

bob said:
Is it possible to dynamically change the location of a module
i.e.
require Plugins::Wetherbys::Generate::GenerateXML;
Plugins::Wetherbys::Generate::GenerateXML->import('help');

works fine

but ideally

i would like

require Plugins::$VAR::Generate::GenerateXML;
Plugins::Wetherbys::$VAR::GenerateXML->import('help');

I have looked through the FAQs and couldn't find anything.
I know you can do require $VAR;

Cheers

perldoc LIB
 
B

Brian McCauley

my $VAR = 'Wetherbys';
my $class = "Plugins::$VAR::Generate::GenerateXML";
( my $module = $class) =~ s{::}{/}g; # File::Spec would be better
$module .= '.pm'; # File::Spec would be better
require $module;
$class->import('help');

This mess may be best stuffed into a sub and taken out of sight.

There is UNIVERSAL::require but I'm not too happy with the fact that
it doesn't re-throw errors.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top