in reply to Re: help with a Perl term
in thread help with a Perl term

Out of curiosity, why would you eval the require, then die if the eval fails? If the require fails it would die anyways. Something like this makes a little more sense (to me):

my $bad; BEGIN { eval { require Foo }; if ($@) { $bad = "Foo"; warn "This requires $bad to work. " . "You can get it at ... "; } } sub new { return undef if $bad; ...etc... }

Cheers,
KM

Replies are listed 'Best First'.
RE: RE: Re: help with a Perl term
by btrott (Parson) on Jun 08, 2000 at 21:06 UTC
    Well... would you believe that it wasn't a very good example? :)

    That said, there are several situations where I might do something like this:

    • When my eval to require the module is wrapped in another eval, where the outer eval is a general exception handler of sorts. In this case, I'd perhaps have an exception handler set up for this particular exception: loading a module failed. So I'd die w/ that particular exception object (using 5.005's (I think) ability to die with an object). I do this often in mod_perl apps... in fact, I probably abuse the technique. :)

    • This is kind of similar to the first example, but oftentimes I'd rather give an explanation of why the module-load failed, rather than just give the standard "can't find in @INC" message. As you did above, in fact.
    What I do wonder is why you're using warn above. If code you're writing absolutely requires a module to work, why wouldn't you want to die? Just wondering.
      I use warn so I get a warning without killing whatever is happening. If you notice, that example is being used as if it were part of a module, and the new() method returns if it's dependant method isn't loaded. This will keep this module from dying when another module/script is trying to use it, while still giving it a warning. For example, we use this technique extensively with the newest version of the Infobot. I have also used it elsewhere. I wouldn't eval a require to just die if it fails, seems redundant.

      Your example would have been better if you showed it in the contect of the first bullet you explain above. :)

      Cheers,
      KM