use/require handler?

Discussion in 'Perl' started by Patrick LeBoutillier, Jan 31, 2004.

  1. Hi all,

    Is there anyway to be notified when the use/require of a module fails?

    Basically I would like to setup a sub that gets called whenever a
    use/require call doesn't find what it is looking for. Sort of an
    AUTOLOAD but for use/require.

    Any ideas?

    Patrick LeBoutillier
    Patrick LeBoutillier, Jan 31, 2004
    #1
    1. Advertising

  2. Patrick LeBoutillier wrote:
    > Is there anyway to be notified when the use/require of a module
    > fails?
    >
    > Basically I would like to setup a sub that gets called whenever a
    > use/require call doesn't find what it is looking for. Sort of an
    > AUTOLOAD but for use/require.
    >
    > Any ideas?


    eval "use Some::Module";
    mysub() if $@;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 31, 2004
    #2
    1. Advertising

  3. Gunnar Hjalmarsson <> wrote in message news:<bsPSb.47162$>...
    > Patrick LeBoutillier wrote:
    > > Is there anyway to be notified when the use/require of a module
    > > fails?
    > >
    > > Basically I would like to setup a sub that gets called whenever a
    > > use/require call doesn't find what it is looking for. Sort of an
    > > AUTOLOAD but for use/require.
    > >
    > > Any ideas?

    >
    > eval "use Some::Module";
    > mysub() if $@;


    That's good, but I would like this to happen transparently behind the scenes, i.e.:

    BEGIN {
    # Hook up the sub "no_such_module" to be called when use/require
    # fails
    }


    use IO::File ; # nothing happens
    use File::Find ; # nothing happens
    use Absent::Module # "no_such_module" called with arg "Absent::Module" ;

    sub no_such_module {
    my $module = shift ;

    warn("Module $module not found.") ;
    }
    Patrick LeBoutillier, Jan 31, 2004
    #3
  4. Gunnar Hjalmarsson <> wrote in message news:<bsPSb.47162$>...
    > Patrick LeBoutillier wrote:
    > > Is there anyway to be notified when the use/require of a module
    > > fails?
    > >
    > > Basically I would like to setup a sub that gets called whenever a
    > > use/require call doesn't find what it is looking for. Sort of an
    > > AUTOLOAD but for use/require.
    > >
    > > Any ideas?

    >
    > eval "use Some::Module";
    > mysub() if $@;


    Never mind. I found it here:

    http://www.perldoc.com/perl5.8.0/pod/func/require.html

    It's new in perl 5.8.0:

    push @INC, \&my_sub;
    sub my_sub {
    my ($coderef, $filename) = @_; # $coderef is \&my_sub
    ...
    }
    Patrick LeBoutillier, Jan 31, 2004
    #4
  5. Gunnar Hjalmarsson, Jan 31, 2004
    #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. shruds
    Replies:
    1
    Views:
    792
    John C. Bollinger
    Jan 27, 2006
  2. Replies:
    1
    Views:
    696
    Damien
    Feb 22, 2007
  3. Replies:
    0
    Views:
    352
  4. ZyLo

    Proper use of 'require'

    ZyLo, Dec 30, 2005, in forum: Ruby
    Replies:
    2
    Views:
    98
    Hal Fulton
    Jan 1, 2006
  5. Servando Garcia

    how to use it: require

    Servando Garcia, Mar 5, 2006, in forum: Ruby
    Replies:
    2
    Views:
    93
    Diego Cano Lagneaux
    Mar 5, 2006
Loading...

Share This Page