Using a package that might not be available

  • Thread starter Frank Burkhardt
  • Start date
F

Frank Burkhardt

Hi everyone,

I'm trying to use a cpan-package but I don't want to depend on it because
sometimes it's unavailable and there's an alternative (which is slower).

<problem>
use strict;
use foo::bar qw(MY_CONSTANT)

if ( defined(foo::bar) ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</problem>

This example will not work, of course - but this one will - at least partly:

<partlysolved>
eval "use foo::bar qw(MY_CONSTANT);"

if ( $@ ne '' ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</partlysolved>

Problems left:
* MY_CONSTANT is not available when "use foo::bar" is done inside eval
* I have a "non-strict"-script now but I like "strict" better :-( .

Does anyone know how to solve one or both of these problems?

Thank you,

Frank
 
P

Paul Lalli

Frank said:
Hi everyone,

I'm trying to use a cpan-package but I don't want to depend on it because
sometimes it's unavailable and there's an alternative (which is slower).

<problem>
use strict;
use foo::bar qw(MY_CONSTANT)

if ( defined(foo::bar) ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</problem>

This example will not work, of course - but this one will - at least partly:

<partlysolved>
eval "use foo::bar qw(MY_CONSTANT);"

if ( $@ ne '' ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</partlysolved>

Problems left:
* MY_CONSTANT is not available when "use foo::bar" is done inside eval
* I have a "non-strict"-script now but I like "strict" better :-( .

Does anyone know how to solve one or both of these problems?

How about separating the two things done by 'use'?

#untested
my $used_foobar = 0;
eval { require foo::bar; };
if ($@) {
warn "Not using foo::bar\n";
} else {
$used_foobar = 1;
foo::bar->import(MY_CONSTANT);
}

Paul Lalli
 
F

Frank Burkhardt

Paul said:
How about separating the two things done by 'use'?

#untested
my $used_foobar = 0;
eval { require foo::bar; };
if ($@) {
warn "Not using foo::bar\n";
} else {
$used_foobar = 1;
foo::bar->import(MY_CONSTANT);
}

Thank you very much - I didn't know the import()-method.

Frank
 
C

ced

Frank said:
Hi everyone,

I'm trying to use a cpan-package but I don't want to depend on it because
sometimes it's unavailable and there's an alternative (which is slower).

<problem>
use strict;
use foo::bar qw(MY_CONSTANT)

if ( defined(foo::bar) ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</problem>

This example will not work, of course - but this one will - at least partly:

<partlysolved>
eval "use foo::bar qw(MY_CONSTANT);"

if ( $@ ne '' ) {
foo::bar::foobar(MY_CONSTANT);
} else {
system("foobar_slow");
}
</partlysolved>

Problems left:
* MY_CONSTANT is not available when "use foo::bar" is done inside eval
* I have a "non-strict"-script now but I like "strict" better :-( .

You should still be able to access the constant via & (perldoc
constant):

eval "use foo::bar qw(MY_CONSTANT)";
unless ( $@ ) {
foo::bar::foobar( &MY_CONSTANT );
} else { # fallback
system( "foobar_slow" );
...
}

hth,
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top