Using a package that might not be available

Discussion in 'Perl Misc' started by Frank Burkhardt, Jan 6, 2006.

  1. 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
    Frank Burkhardt, Jan 6, 2006
    #1
    1. Advertising

  2. Frank Burkhardt

    Paul Lalli Guest

    Frank Burkhardt wrote:
    > 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
    Paul Lalli, Jan 6, 2006
    #2
    1. Advertising

  3. Paul Lalli wrote:

    > 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
    Frank Burkhardt, Jan 6, 2006
    #3
  4. Frank Burkhardt

    Guest

    Frank Burkhardt wrote:
    > 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,
    --
    Charles DeRykus
    , Jan 6, 2006
    #4
  5. Frank Burkhardt <> wrote:
    > Paul Lalli wrote:
    >
    >> How about separating the two things done by 'use'?


    > I didn't know the import()-method.



    You should read the documentation for the functions that you use:

    perldoc -f use


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 6, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    2,569
    Natty Gur
    Jun 6, 2004
  2. =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=
    Replies:
    0
    Views:
    2,382
    =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?=
    Jun 7, 2004
  3. TR
    Replies:
    1
    Views:
    386
    John Saunders
    Dec 14, 2004
  4. moon
    Replies:
    13
    Views:
    6,599
    Gregory A. Swarthout
    Mar 5, 2004
  5. Robbie Baldock

    PHP 5.0 (not as OT as it might appear)

    Robbie Baldock, Jul 15, 2004, in forum: Java
    Replies:
    9
    Views:
    358
    Roedy Green
    Jul 16, 2004
Loading...

Share This Page