SPUG: Answered my own question
Creede Lambard
creede at rrauto.com
Fri Feb 16 10:08:51 CST 2001
Thanks for the reply. I was going by an example in the Perl Cookbook,
section 12.2, "Trapping Errors in Require or Use." But, after I posted the
second message I found that I wasn't supposed to put the string in a block,
which made me feel even less intelligent and wishing I hadn't posted the two
mails to the list. The sample they give is
BEGIN {
unless (eval "require $mod") {
warn "couldn't load $mod: $@";
}
}
But I have something that works now, which I hope makes this a learning
experience, except that I probably won't have learned to think about five
more minutes before the next time I click the "Send" button.
-- Creede
----- Original Message -----
From: Jeremy Devenport <jeremy at weezel.com>
To: Creede Lambard <creede at rrauto.com>; <spug-list at pm.org>
Sent: Thursday, February 15, 2001 8:32 PM
Subject: RE: SPUG: Answered my own question
>
> 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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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