| 1 | package Sidef::Types::Block::Fork; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use parent qw( | ||
| 7 | Sidef::Object::Object | ||
| 8 | ); | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my (undef, %opts) = @_; | ||
| 12 | bless \%opts, __PACKAGE__; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub get { | ||
| 16 | my ($self) = @_; | ||
| 17 | |||
| 18 | # Wait for the process to finish | ||
| 19 | waitpid($self->{pid}, 0); | ||
| 20 | |||
| 21 | # Return when the fork doesn't hold a file-handle | ||
| 22 | exists($self->{fh}) or return undef; | ||
| 23 | |||
| 24 | # Rewind at the beginning | ||
| 25 | seek($self->{fh}, 0, 0); | ||
| 26 | |||
| 27 | # Get the content | ||
| 28 | my $content = do { | ||
| 29 | local $/; | ||
| 30 | readline($self->{fh}); | ||
| 31 | }; | ||
| 32 | |||
| 33 | # Evaluate the result | ||
| 34 | my @result = (eval($content)); | ||
| 35 | $@ && die "[FORK ERROR] can't retrieve value: $@"; | ||
| 36 | wantarray ? @result : $result[-1]; | ||
| 37 | } | ||
| 38 | |||
| 39 | *wait = \&get; | ||
| 40 | *join = \&get; | ||
| 41 | |||
| 42 | sub kill { | ||
| 43 | my ($self, $signal) = @_; | ||
| 44 | kill(defined($signal) ? "$signal" : 'KILL', $self->{pid}); | ||
| 45 | } | ||
| 46 | |||
| 47 | 1 |