Search code examples
perlforkanyevent

Errors with AnyEvent in Windows Perl


I try to use AnyEvent's fork_call in Windows Perl. I wrote some sample code:

use AnyEvent;
use AnyEvent::Util;
use Data::Dumper;

my $cv = AnyEvent -> condvar;
my $a;

fork_call {
    $a = 1;
    $cv -> send;
}, sub {
    $cv -> recv;
    print Dumper $a;
}

But I got following error:

Can't locate object method "one_event" via package "AnyEvent" at
C:/Perl/perl/site/lib/AnyEvent/Util.pm line 329.
END failed--call queue aborted.    

Solution

  • Your code is wrong. If you using fork() then you'll have copy of $cv and $a. Right code is:

    use AnyEvent;
    use AnyEvent::Util;
    
    my $cv = AnyEvent->condvar;
    my $a;
    
    fork_call {
        1;
    } sub {
        ($a) = @_;
        print($a);
        $cv->send();
    };
    $cv->wait();