0

I'm using the following perl code on windows environment:

use Time::HiRes qw(usleep);

    #(some code here)

$self->{GLOBAL_OBJ}->xsleep($delay) if($delay);


sub xsleep {    
    my $seconds = shift;
    #print "will sleep:$seconds seconds\n";
    $seconds = $seconds * 1000000;
    usleep($seconds);
    #print "slept:$seconds micro seconds\n";

    return 0;
}

When I call xsleep like that (from another module) the system is stuck and I can only stop it by ctrl+c, however when I call it from the current module it works fine.

Can anyone tell me why this is and how can I fix it? Thanks

Omer Levy
  • 51
  • 2
  • 2
    If you are going to sleep in units of seconds, why not just use sleep (second resolution)? You may also need to accept the param as `my ($self, $seconds) = @_;` in the first line of your xsleep subroutine. – xxfelixxx Sep 19 '17 at 06:03
  • 1
    thanks @xxfelixxx, it was the $self issue. – Omer Levy Sep 19 '17 at 06:10

1 Answers1

1

xsleep is being called as a method, which means the invocant (the result of the left side of ->) is passed as the first argument. This is currently ending up in $seconds. References numify to their address, so you get an extremely large numbers in $seconds. For example,

$ perl -e'CORE::say(0+{})'
9304720

Either adjust xsleep so it can be called as a method,

$self->{GLOBAL_OBJ}->xsleep($delay) if $delay;

sub xsleep {    
    my $self    = shift;
    my $seconds = shift;
    ...
}

or call xsleep as a sub

The::Package::xsleep($delay) if $delay;

sub xsleep {    
    my $seconds = shift;
    ...
}
ikegami
  • 367,544
  • 15
  • 269
  • 518