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.

Dominik

---

Bekanntgabe:
Bitte in Zukunft sämtliche E-mailkorrespondenz an diese E-Mailadresse:
domi...@foop.at

Dominik Danter

Reply via email to