Subject: | wrong constant intermediate exit code |
I have trivially modified an example from
http://search.cpan.org/~glai/IPC-Exe-1.005/lib/IPC/Exe.pm
, the one under
"
Here is an elaborate example to pipe STDOUT of one process to the STDIN
of another, consecutively:
".
The command line was:
/mnt/sdb8/sergei/QEMU/portable_perl/try_ipc_exe.pl -ltr
and its screen output was:
"
Show quoted text
last_pipe> sort -nk1
in-between exit value: 16777215
8 read_in> -rwxr-xr-x 1 sergei users 517 2009-04-15 23:22 processor.pl
111 read_in> prw-r--r-- 1 sergei users 0 2009-04-15 23:22 my_fifo
443 read_in> -rw------- 1 sergei users 518 2009-04-15 23:22 processor.pl.bck
859 read_in> total 8
$exitval=0 at /mnt/sdb8/sergei/QEMU/portable_perl/try_ipc_exe.pl line
60, <STDIN> line 4.
".
The problem is this:
in-between exit value: 16777215
line.
I believe the line should reflect the value supplied to 'exit' in this
line of my script:
exe qw(perl -we), 'while(<STDIN>){my $t = int(rand(1000));print "$t
Show quoted textread_in> $_"} exit(123)',
, however, the 16777215 value is the same regardless of what I supply to
'exit'. It stays the same even if 'exit(0)' is used.
Perl is self-built 5.10.0, built with self-built gcc-4.3.3. The OS is 32
bits PC linux (SUSE 10.3):
Linux amdam2 2.6.22.19-0.2-default #1 SMP 2008-12-18 10:17:03 +0100 i686
athlon i386 GNU/Linux
.
I have used this Perl executable for a number a weeks and haven't
noticed anything unusual, so I do not think the problem is related to my
build actions.
My modifications are:
1) use strict;
2) giving a value to $program (/bin/ls);
3) a number of 'warn' statements here and there;
4) indentation.
Subject: | try_ipc_exe.pl |
#!/home/sergei/PERL-5.10.0/bin/perl -w
use IPC::Exe qw(exe bg);
use strict;
$| = 1;
my $program = '/bin/ls';
my @pids =
&{
# redirect STDERR to STDOUT
exe sub { "2>&1" }, $program, @ARGV,
# 'perl' receives STDOUT of $program via STDIN
exe sub
{
my ($pid) =
&{
exe qw(perl -we), 'while(<STDIN>){my $t = int(rand(1000));print "$t read_in> $_"} exit(123)',
};
# check if exe() was successful
defined($pid) or die("Failed to fork process");
# handle exit value here
warn "in-between exit value: " . ($? >> 8) . "\n";
# this is executed in child process
# no need to return
},
# 'sort' receives STDOUT of 'perl'
exe qw(sort -nk1),
# [perlsub] receives STDOUT of 'sort'
exe sub
{
# find out command of previous pipe process
# if @_ is empty list, previous process was a [perlsub]
my ($prog, @args) = @_;
print STDERR "last_pipe> $prog @args\n"; # output: "last_pipe> sort -n"
# print sorted, 'perl' filtered, output of $program
print while <STDIN>;
# find out exit value of previous 'sort' pipe process
close($IPC::Exe::PIPE);
warn("Bad exit for: @_\n") if $?;
return $?;
},
};
# check if exe()'s were successful
defined($pids[0]) && defined($pids[1]) && defined($pids[2]) or die("Failed to fork processes");
# obtain exit value of last process on pipeline
my $exitval = pop(@pids) >> 8; warn "\$exitval=$exitval";