#! /usr/bin/perl -w use strict; use threads; sub calculation { print("calculating something\n"); 5; } # Starting a co-process to read from. my $procid = open( READ, '-|', 'perl -e "$|=1; for (1..10) {print \"Line $_\n\"; sleep 1;}"' ); ##switch the order of the previous and next lines and the crash does not occur. my $thread = async(\&calculation); # Reading from co-process. until ( 0 ) { my $line = ; last unless defined $line; print $line; } close(READ); print $thread->join() ."\n";