perlanyeventperl5.18

Installed module not found when running program


Context

Here is a perl test script, in which I wanted to see how you can use a specific event loop with AnyEvent :

# file test.pl :
#!/usr/bin/perl

use strict;
use warnings;

use AnyEvent;
use AnyEvent::Impl::EV;

my $cv = AnyEvent->condvar;

my $wait_one_and_a_half_seconds = AnyEvent->timer (
  after => 0.5,  # after how many seconds to invoke the cb?
  cb    => sub { # the callback to invoke
     print ("Hello from callback\n");
     $cv->send;
  },
);

# now wait till our time has come
$cv->recv;

Problem

Here is the error I get when running the above code :

$ perl test.pl
Can't locate EV.pm in @INC (you may need to install the EV module) (@INC
contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2
/usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18
/usr/local/lib/site_perl .) at /usr/local/lib/perl/5.18.2/AnyEvent/Impl/EV.pm
line 28.
BEGIN failed--compilation aborted at /usr/local/lib/perl/5.18.2/AnyEvent/Impl/EV.pm line 28.
Compilation failed in require at test.pl line 6.
BEGIN failed--compilation aborted at test.pl line 6.

Yet I installed the AnyEvent package using cpanm, and the AnyEvent/Impl/EV.pm file is present in one of the @INC path :

$ ls /usr/local/lib/perl/5.18.2/AnyEvent/Impl/
Cocoa.pm     Event.pm  FLTK.pm  IOAsync.pm  Perl.pm  Qt.pm  UV.pm
EventLib.pm  EV.pm     Glib.pm  Irssi.pm    POE.pm   Tk.pm

Question

How do I fix this ?

Extra remark

The error message says it is looking for EV.pm, but I would have expected AnyEvent/Impl/EV.pm.
How come the use AnyEvent::Impl::EV; I wrote got turned into perl is looking for EV.pm at runtime ?


Solution

  • The error message was actually a very correct and forward pointer to what should be done : there is an EV package which needs to be installed separately :

    $ sudo cpanm EV
    --> Working on EV
    Fetching http://www.cpan.org/authors/id/M/ML/MLEHMANN/EV-4.18.tar.gz ... OK
    Configuring EV-4.18 ... OK
    Building and testing EV-4.18 ... OK
    Successfully installed EV-4.18
    1 distribution installed
    

    After that, everything works :

    $ cat test.pl 
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use AnyEvent;
    use EV;
    
    my $wait_one_and_a_half_seconds = AnyEvent->timer (
      after => 0.5,  # after how many seconds to invoke the cb?
      cb    => sub { # the callback to invoke
         print ("Hello from callback\n");
      },
    );
    
    # now wait till our time has come
    EV::run();
    
    $ perl test.pl 
    Hello from callback