SPUG: Answered my own question

Jeremy Devenport jeremy at weezel.com
Thu Feb 15 22:32:45 CST 2001


>From 'perldoc eval':
    In both forms, the value returned is the value of
    the last expression evaluated inside the mini-pro-
    gram

I think what you actually want to do is:

BEGIN {
    eval {require FOO}; # this could just as easily be 'use' instead of
'require' but this skips trying to import
    if ($@) { # use $@ to catch exceptions from the eval
        $hasfoo = 0;
    }
    else {
        $hasfoo = 1;
    }
}

In your second attempt you just happened to get the right result, it is
entirely coincidental. Try switching the module name to something like
"frobaz" and see if you still get what you expect. What you are actually
doing is setting $hasQuote based on whether or not the string "use
Finance::YahooQuote" is a true value, most strings are.

Jeremy

-----Original Message-----
From: owner-spug-list at pm.org [mailto:owner-spug-list at pm.org]On Behalf Of
Creede Lambard
Sent: Thursday, February 15, 2001 12:44 PM
To: spug-list at pm.org
Subject: SPUG: Answered my own question


Here's the code that works:

BEGIN
{
    $hasQuote = 1;
    unless ( eval { "use Finance::YahooQuote" })        # note the quotes!!
        { $hasQuote = 0; }
}

print $hasQuote;



The trick was using a STRING in the eval rather than a straightforward use
statement.

Now I feel silly, but thanks anyway.

-- Creede


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/




 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list