Conditional 'use' depending of a variable rather than a constant ?

Y

Yohan N. Leder

Hello. Does it exists a method to "include" a module depending of a
scalar value ?

When I want to include one depending of a constant value (i.e. a value
which already exist at compile-time), I do something like :

BEGIN{if (FLAG){require Thing; import Thing qw(trick dummy);}}

But what if the constant FLAG becomes a variable $FLAG ? A code like
this same above (replacing FLAG with $FLAG) dives a ligitimate "use of
uninitialized value...", just because $FLAG is only initialized at
runtime)

Is there a way to achieve this : conditional "use" of a module based on
a variable value ?
 
J

Jürgen Exner

Dr.Ruud said:
Yohan N. Leder schreef:


See `perldoc if`.

Maybe you have a different version of perl, but I am getting
No documentation found for "if".

Besides, "use" is executed at compile time. If you want to make it dependent
on the value of a variable, then you have to delay the import until runtime
because (typically) you won't know the value of the variable until runtime.

I think the OP is actually looking for the documentation of use()
(perldoc -f use) where the behaviour of use() is described in detail.

jue
 
P

Paul Lalli

Jürgen Exner said:
Maybe you have a different version of perl, but I am getting
No documentation found for "if".
http://search.cpan.org/~ilyaz/if-0.0401/if.pm
http://perldoc.perl.org/if.html

Besides, "use" is executed at compile time. If you want to make it dependent
on the value of a variable, then you have to delay the import until runtime
because (typically) you won't know the value of the variable until runtime.

This, however, is still true.

Paul Lalli
 
S

Sherm Pendley

Yohan N. Leder said:
Hello. Does it exists a method to "include" a module depending of a
scalar value ?

When I want to include one depending of a constant value (i.e. a value
which already exist at compile-time), I do something like :

BEGIN{if (FLAG){require Thing; import Thing qw(trick dummy);}}

But what if the constant FLAG becomes a variable $FLAG ? A code like
this same above (replacing FLAG with $FLAG) dives a ligitimate "use of
uninitialized value...", just because $FLAG is only initialized at
runtime)

The BEGIN above is what forces the require to execute at compile-time -
that and calling import() is the difference between require and use. Use
executes at compile-time and calls import(), while require does not.

So the answer is simple: If you don't want to call require() at compile-
time, then don't call require() at compile-time. :)

Also, if you want to optionally use a module if it's available, and simply
set a flag if it's not, you can wrap the require in a block eval to trap
errors.

unless (eval {
if (FLAG) {
require Thing;
import Thing qw(trick dummy);
}
}) {
die "Thing could not be required: $@";
}

sherm--
 
J

John Bokma

Sherm Pendley said:
Also, if you want to optionally use a module if it's available, and
simply set a flag if it's not, you can wrap the require in a block
eval to trap errors.

unless (eval {
if (FLAG) {
require Thing;
import Thing qw(trick dummy);
}
}) {
die "Thing could not be required: $@";
}


Uhm how about:

if ( $flag ) {

eval "use Thing qw(trick dummy)";
$@ and die $@;
}
 
U

Uri Guttman

JB> Uhm how about:

JB> if ( $flag ) {

JB> eval "use Thing qw(trick dummy)";
JB> $@ and die $@;
JB> }

unless that is in a BEGIN block those exported subs will not be seen by
the compiler and so will be useless. in that case you might as well call
require which is what i do for runtime loaded plugins which must be OO
so they don't need to export symbols.

the whole issue of conditional compile time exporting is tricky and i
suspect is actually rarely needed. by using OO design you can do the
loading at runtime as i mentioned. or if you must do it at compile time
use the if pragma or do it in a BEGIN block. and you really have to
justify that craziness to me. i just would rather design code that
didn't need that.

uri
 
S

Sherm Pendley

John Bokma said:
Uhm how about:

if ( $flag ) {

eval "use Thing qw(trick dummy)";
$@ and die $@;
}

Just as good. I generally avoid string eval() when possible, but that's
just a matter of personal preference and habit.

sherm--
 
J

John Bokma

Uri Guttman said:
JB> Uhm how about:

JB> if ( $flag ) {

JB> eval "use Thing qw(trick dummy)";
JB> $@ and die $@;
JB> }

unless that is in a BEGIN block those exported subs will not be seen by
the compiler and so will be useless.

Aargh true. I did use the above in a project (unconditional though), which
worked because I didn't export things :-D.
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top