By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

use/require handler?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<bs********************@newsb.telia.net>...
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.") ;
}
Jul 19 '05 #3

P: n/a
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<bs********************@newsb.telia.net>...
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
...
}
Jul 19 '05 #4

P: n/a
Patrick LeBoutillier wrote:
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
...
}


Hmm.. Interesting possibility that I was not aware of. Thanks for the
tip. :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.