Thread Systemaufruf, kehrt nicht zurück (15 answers)
Opened by theresa at 2008-03-03 15:45

heihon
 2008-03-18 12:16
#107173 #107173
User since
2006-09-15
15 Artikel
BenutzerIn
[default_avatar]
Ich habe unter Windows folgendes versucht:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/perl -w
#-----------------------------------------------------------------------------

use strict;

my $cmd = 'perl -e "sleep 1;print STDOUT qq(Hallo 1),$/;print STDERR qq(Hallo 2),$/"';
my $text = "$cmd 2>&1";
my $return;

sub mit_system {
    eval {
        local $SIG{ALRM} = sub { die "alarm\n" };       # NB \n required
        alarm 2;
        $return = system($text);
        alarm 0;
    };
    die if $@ && $@ ne "alarm\n";       # propagate errors
    if ($@) {
        # timed out
        print "\n\$\@ = $@\ntimeout, return: $return\n";
    }
    else {
        # didn't
        print "\nreturn: $return\n";
    }
}

sub mit_qx {
    eval {
        local $SIG{ALRM} = sub { die "alarm\n" };       # NB \n required
        alarm 2;
        $return = qx($text);
        alarm 0;
    };
    die if $@ && $@ ne "alarm\n";       # propagate errors
    if ($@) {
        # timed out
        print "\n\$\@ = $@\ntimeout, return: $return\n";
    }
    else {
        # didn't
        print "\nreturn: $return\n";
    }
}

mit_system();

mit_qx();

Wenn Du für den Programmaufruf
Code: (dl )
system()
verwendest, erhältst Du den Fehlercode, den das Programm liefert, als Ergebnis. Wenn es keinen Fehler gibt, erhältst Du die 0 (Null).

Wenn Du für den Programmaufruf
Code: (dl )
qx()
verwendest, erhältst Du die Ausgabe des Programmes (das, was das Programm nach STDOUT bzw. in die Konsole schreibt) als Ergebnis. Ich nehme an, dass Du eher an Letzterem interessiert bist.

View full thread Systemaufruf, kehrt nicht zurück