4

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 ?

Borodin
  • 126,100
  • 9
  • 70
  • 144
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • What user are you running as, and what do the ownership/permissions look like in that directory? – Sobrique Dec 17 '14 at 14:01
  • @Sobrique : permissions are OK, the user is my account and the path is opened to anyone – LeGEC Dec 17 '14 at 14:04
  • Oh and this is probably wrong: `#!/usr/bin/perl` - you're using `/usr/local`. – Sobrique Dec 17 '14 at 14:04
  • Might try rerunning `cpan install AnyEvent::Impl::EV` to see if it needs to. But just tried it and reproduced it. – Sobrique Dec 17 '14 at 14:08
  • 1
    I always use `#!/usr/bin/env perl` as the first line. – reinierpost Dec 17 '14 at 14:11
  • @toolic : I first tried `use EV;`, got the error `package EV.pm not found`, then tried the full path. As I wrote in the remark, I am surprised to see the interpreter looks for `EV.pm` and not `AnyEvent/Impl/EV.pm`. There is some mechanism that I am not aware of. – LeGEC Dec 17 '14 at 14:14
  • 1
    It's not your code doing it. Look at the EV.pm module, and on line 28 it has got: `use EV 4.00`. Which is ... loading itself, it seems? – Sobrique Dec 17 '14 at 14:16
  • What version of perl are you using? I think that's at fault here. Doesn't work on my `5.8.5` instance, works on my `5.20.1` instance. – Sobrique Dec 17 '14 at 14:23
  • Nevermind, you're having fun with 5.18. Would be surprised to find a different exists in such a recent version, but it wouldn't be the first time. – Sobrique Dec 17 '14 at 14:36

2 Answers2

1

Just tried to reproduce this with cpan install AnyEvent and can confirm I get the same error.

Line 28 of 'EV.pm' is use EV 4.00;. Your use EV; is a bit of a red herring - that's not the source of the error. This module explicitly includes a 'use' line (which frankly is a bit wierd, it's 'using' itself it seems?)

I don't think that's ever going to work, unless the @INC path is changed - I can only assume that the loading of this module is handled elsewhere, without deconstructing source code.

Referencing the man page - this module gets loaded automatically as required. So you probably don't need to use it in the first place.

Edit: Just compared perl versions. Perl 5.8.5 shows the same behaviour. My 5.20.1 install doesn't.

I'm not sure upgrading perl is necessarily the right step, but it might be worth trying? I'll try and figure out why 5.20.1 works though. It's got to be something to do with handling of @INC.

Edit:

"The handling of return values of @INC filters (subroutines returned by subroutines in @INC) has been fixed in various ways. Previously tied variables were mishandled, and setting $_ to a reference or typeglob could result in crashes."

http://perldoc.perl.org/perl5200delta.html

I think that might be what the problem is.

You're certainly not alone in having this: http://www.cpantesters.org/cpan/report/d5939816-a510-11e0-bd04-22322d9f2468

From: http://cpansearch.perl.org/src/MLEHMANN/AnyEvent-7.08/Changes

5.29 Sun Dec 5 10:49:21 CET 2010 - convert EV backend to EV 4.00 API (so better upgrade EV too).

Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • I'm using 5.18. Indeed, using another event loop (for example `AnyEvnt::Impl::Perl`) works. – LeGEC Dec 17 '14 at 14:44
  • Yup. Perldelta. Edited. – Sobrique Dec 17 '14 at 14:46
  • Thx thx thx thx (<- the min chars turns polite people into extra-polite people) – LeGEC Dec 17 '14 at 14:47
  • AnyEvent::Impl::Perl doesn't have that 'use' line in there. That seems to be the culprit, but short of getting hacky with module code (which might work I guess...) the best answer might be upgrading Perl. – Sobrique Dec 17 '14 at 14:47
  • So given EV was changed on Dec 5th, it might be worth loading an older version. Manually download from CPAN and install. Or upgrade perl. – Sobrique Dec 17 '14 at 14:59
1

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
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Well, technically you _did_ find a bug, it's just one that's since been fixed. (And oddly, I've still got because of the version I've installed... Having said that, `EV.pm` isn't the same as `AnyEvent::Impl::EV.pm` – Sobrique Dec 17 '14 at 15:01
  • 1
    Please mark your solution as accepted so that others can see that your question has been solved. – Borodin Dec 17 '14 at 15:36