use/require handler?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick LeBoutillier

    use/require handler?

    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
  • Gunnar Hjalmarsson

    #2
    Re: use/require handler?

    Patrick LeBoutillier wrote:[color=blue]
    > 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?[/color]

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

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

    Comment

    • Patrick LeBoutillier

      #3
      Re: use/require handler?

      Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<bsPSb.471 62$mU6.179806@n ewsb.telia.net> ...[color=blue]
      > Patrick LeBoutillier wrote:[color=green]
      > > 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?[/color]
      >
      > eval "use Some::Module";
      > mysub() if $@;[/color]

      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.") ;
      }

      Comment

      • Patrick LeBoutillier

        #4
        Re: use/require handler?

        Gunnar Hjalmarsson <noreply@gunnar .cc> wrote in message news:<bsPSb.471 62$mU6.179806@n ewsb.telia.net> ...[color=blue]
        > Patrick LeBoutillier wrote:[color=green]
        > > 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?[/color]
        >
        > eval "use Some::Module";
        > mysub() if $@;[/color]

        Never mind. I found it here:



        It's new in perl 5.8.0:

        push @INC, \&my_sub;
        sub my_sub {
        my ($coderef, $filename) = @_; # $coderef is \&my_sub
        ...
        }

        Comment

        • Gunnar Hjalmarsson

          #5
          Re: use/require handler?

          Patrick LeBoutillier wrote:[color=blue]
          > 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
          > ...
          > }[/color]

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

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

          Comment

          Working...