die $!; # me 2> file.txt open STDERR, ">", "file.txt" or die $!; # me < file.txt open STDIN, "<", "file.txt" or die $!; # me >> file.txt open STDOUT, ">>", "file.txt" or die $!; ϑΝΠϧʹϦμΠϨΫτ͢Δʹ طଘϑΝΠϧϋϯυϧΛ͏Ұopen͢Δ
$write; my $pid = fork // die $!; if ($pid == 0) { # ls -al close $read; open STDOUT, ">&", $write; exec "ls", "-al"; exit 255; } else { # me close $write; my @got = <$read>; warn "got: $_" for @got; waitpid $pid, 0; close $read; } # me | wc -l pipe my $read, my $write; my $pid = fork // die $!; if ($pid == 0) { # wc -l close $write; open STDIN, "<&", $read; exec "wc", "-l"; exit 255; } else { # me close $read; say {$write} "line1"; say {$write} "line2"; close $write; waitpid $pid, 0; }
"wc", "-l" or die $!; say {$fh} "line1"; say {$fh} "line2"; close $fh; # automatically call wait() # ls -al | me open my $fh, "-|", "ls", "-al" or die $!; my @got = <$fh>; close $fh; # automatically call wait() warn "got: $_" for @got;
@cmd = ("perl", "-E", '$|++; for (1..10) { say "$$: $_"; sleep int(rand 5); }’); # @cmdͷग़ྗΛ͖ͨͷ͔Β࣌ࠁΛઌ಄ʹ͚ͭͯग़ྗ open my $fh0, "-|", @cmd or die $!; open my $fh1, "-|", @cmd or die $!; my $select = IO::Select->new($fh0, $fh1); while (1) { last if $select->count == 0; my @ready = $select->can_read(1); for my $fh (@ready) { my $line = <$fh>; defined $line or do { $select->remove($fh); next }; my $time = strftime("%F %T", localtime); print "$time: $line"; } } close $_ for $fh0, $fh1;