ViceRaid has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to use a BEGIN block (or something) in a required file to check whether another module's available. If it's not, I'd like to give up processing this file and just return a true value to the require. I'm not really sure how to do it: something like this?

BEGIN { eval "require Maybe::Module"; if ( $@ ) { # don't bother } } # I only want these to be defined if Maybe::Module's available sub foo { ... }

Thanks
ViceRaid

Replies are listed 'Best First'.
Re: Aborting loading of file in a BEGIN block
by broquaint (Abbot) on Aug 15, 2003 at 15:33 UTC
    Put a block eval around the initial require, and in the file that it gets passed just use the desired module e.g
    # in calling file eval { require Your::Module }; require FailSafe::Module if $@; ## Your/Module.pm ## # BEGIN + require + no import use Maybe::Module ();

    HTH

    _________
    broquaint

      Cheers. Unfortunately the require-ing's being done in a context over which I don't have control. I can't alter the code there, I can only change the require-ee.

      ViceRaid

        It sounds like you need to rethink your approach as this isn't simple stuff I'm afraid. There isn't, too my knowledge, a way of avoiding compiling code in a file, short of die()ing in the BEGIN block or some other such hackery. So if you're not too bothered about a bit of symbol table munging, and you want the subs in the current package to disappear on the case of failure then something like this might do
        BEGIN { eval { require Maybe::Module }; $::SPLAT = 1 if $@; } ## *right* at the end of the file if($::SPLAT) { require Symbol and Symbol->import('delete_package'); delete_package(__PACKAGE__); } 1;
        Now in the event of the failure to require the given module the current package will be deleted in it's entirety. If the current package is main or someone else's package, then stick a package Other::Package::__temp at the top and export the subroutines upon success e.g
        package Other::Package::__temp; BEGIN { eval { require Maybe::Module }; $::SPLAT = 1 if $@; } ## *right* at the end of the file if($::SPLAT) { require Symbol and Symbol->import('delete_package'); delete_package(__PACKAGE__); } else { @{caller()."::"}{keys %{__PACKAGE__."::"}} = values %{__PACKAGE__."::"}; } 1;
        That will export the globs in the current package to the caller's package, which should do the trick.
        HTH

        _________
        broquaint

Re: Aborting loading of file in a BEGIN block
by John M. Dlugosz (Monsignor) on Aug 15, 2003 at 18:57 UTC
    Instead of a lone 1; at the end of a module, you can use a return 1; statement for the same purpose. So, does that imply that if a return is found other than at the end, it will not process what comes after? That may be useful.

Re: Aborting loading of file in a BEGIN block
by bdimych (Monk) on Oct 20, 2007 at 02:04 UTC
    I'd found this old post when I was finding solution for the same problem. Just now I've learned "man perlfilter" and it seems exactly what is needed - it allows to do not parse module at all depending on some custom conditions. Example from "man perlfilter" works well.