Skip to content

Commit

Permalink
- Improved the Block.fork() to use an anonymous temporary file, provi…
Browse files Browse the repository at this point in the history
…ded by Perl's open() function
  • Loading branch information
trizen committed Aug 18, 2015
1 parent 1275bb3 commit 7923227
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 4 additions & 8 deletions lib/Sidef/Types/Block/Code.pm
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,14 @@ package Sidef::Types::Block::Code {
sub fork {
my ($self) = @_;

state $x = do {
require Storable;
require File::Temp;
};

my ($fh, $filename) = File::Temp::tempfile(SUFFIX => '.rst');
my $fork = Sidef::Types::Block::Fork->new(result => $filename);
state $x = require Storable;
open(my $fh, '+>', undef); # an anonymous temporary file
my $fork = Sidef::Types::Block::Fork->new(fh => $fh);

my $pid = fork() // die "[FATAL ERROR]: cannot fork";
if ($pid == 0) {
srand();
Storable::store($self->run, $filename);
Storable::store_fd($self->run, $fh);
exit 0;
}

Expand Down
10 changes: 4 additions & 6 deletions lib/Sidef/Types/Block/Fork.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ package Sidef::Types::Block::Fork {
# Wait for the process to finish
waitpid($self->{pid}, 0);

# Return when the fork doesn't hold a result
exists($self->{result})
or return;
# Return when the fork doesn't hold a file-handle
exists($self->{fh}) or return;

state $x = require Storable;
my $ref = eval { Storable::retrieve($self->{result}) };
unlink(delete $self->{result});
$ref;
seek($self->{fh}, 0, 0); # rewind at the beginning
scalar eval { Storable::fd_retrieve($self->{fh}) };
}

*wait = \&get;
Expand Down

0 comments on commit 7923227

Please sign in to comment.