On Thu, Mar 21, 2013 at 6:41 PM, Charles DeRykus <dery...@gmail.com> wrote: > On Thu, Mar 21, 2013 at 3:12 PM, Dominik Danter <domi...@foop.at> wrote: >> Hi I just don't understand the perlfaq example. All I want is to capture >> output >> of an external command's stdout and stderr. Here is what I've tried: >> >> >> sub get_exit() { >> my ($exit_status, $std_out, $std_err) = @_; >> my %error_codes = ( >> 1024 => 'uid already exists', >> 256 => 'not root', >> 0 => 'success', >> ); >> my $message = $error_codes{$exit_status}; >> return { >> message => (defined $message ? $message : 'unknown exit status'), >> exit_status => $exit_status, >> std_out => (defined $std_out ? $std_out : 'no standard output >> produced'), >> std_err => (defined $std_err ? $std_err : 'no error output >> produced'), >> }; >> } >> >> sub change_id { >> my %command = ( >> uid => 'usermod --uid', >> gid => 'usermod --gid', >> ); >> my ($type, $username, $new_id) = @_; >> my ($std_in, $std_out, $std_err, $exit_status); >> eval { >> my $pid = open3($std_in, $std_out, $std_err, >> "$command{$type} $new_id $username"); >> waitpid( $pid, 1); >> $exit_status = $? >> 8; >> }; >> return &get_exit($exit_status, $std_out, $std_err ); >> } >> >> >> print Dumper(user::change_id('uid','bla',997)); >> >> >> Here is my output: >> Use of uninitialized value $exit_status in hash element at user.pm line 31. >> $VAR1 = { >> 'std_err' => 'no error output produced', >> 'std_out' => \*Symbol::GEN1, >> 'exit_status' => undef, >> 'message' => 'unknown exit status' >> }; >> >> >> The command should fail since it lacks required permission, but I'd like to >> handle the error messages gracefully. > > waitpid( $pid, 1); > ^^^ > > This looks amiss. Normally, it's waitpid($pid,0) for a blocking > wait or maybe POSIX's WNOHANG. for an async wait. See > perldoc -f waitpid. > > -- > Charles DeRykus.
-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/