Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Understanding $?

by manishrathi (Beadle)
on Mar 06, 2013 at 22:28 UTC ( [id://1022109]=perlquestion: print w/replies, xml ) Need Help??

manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

......if(defined($filelist)){ $cmd = "$odhome\\bin\\iwodstart $iwodclient_cfg -k srcArea=$so +urcePath -k targetArea=$targetPath -k definition=$deploymentDefinitio +n -k workareaName=$workareaName -k filelist=$filelist -inst $$ 2>&1"; }elsif(defined($workareaName)){ $cmd = "$odhome\\bin\\iwodstart $iwodclient_cfg -k srcArea=$so +urcePath -k targetArea=$targetPath -k definition=$deploymentDefinitio +n -k workareaName=$workareaName -inst $$ 2>&1"; }else{ $cmd = "$odhome\\bin\\iwodstart $iwodclient_cfg -k srcArea=$so +urcePath -k targetArea=$targetPath -k definition=$deploymentDefinitio +n -inst $$ 2>&1"; } @output = `$cmd`; print "CMD = $cmd <br>"; #Check for the success/failure of the deployment process. $rc = $?; my $success_flag = 0; foreach (@output) { $success_flag = 1 if (/ERROR/i); $success_flag = 1 if (/Status: Failed/i); } ...


In this code, isn't $? supposed to be mentioned in $cmd ? If its not mentioned there, will it still obtain error value from the execution of $cmd at command line ? Will this $cmd return any value if deployment is successful or not ?
If there is another execution on cmd line in the code, what error value will be represented by $? ?

Thanks

Replies are listed 'Best First'.
Re: Understanding $?
by rjt (Curate) on Mar 07, 2013 at 01:24 UTC

    For a more thorough treatment of what $? will contain, have a look at POSIX's macros for WIFEXITED and WEXITSTATUS. The canonical (but admittedly not always followed!) way of checking an exit status is something like:

    my $output = `echo hello`; # Output contains "hello" if (WIFEXITED($?)) { print("Command exited with status " . WEXITSTATUS($?)); } else { print("Command exited abnormally"); # If desired, use the other macros to figure out which # signal killed the process. }

    Obviously in the above contrived example with echo, it's highly unlikely to exit abnormally, but with more complex commands, it can be very helpful to know what killed the sub-process, and what the exit status was.

    Of course, the $output variable will contain the actual standard output (STDOUT) of the command itself, if there was any. If you need to capture STDERR in the same variable as well, you can add 2>&1 to the end of your command. If you need to capture STDOUT and STDERR separately, you would need to use pipes (a la IPC::Open3 rather than the simple backticks (qx) operator, or redirect the streams to File::Temp files and read them in after the command finishes.

Re: Understanding $?
by kennethk (Abbot) on Mar 06, 2013 at 22:44 UTC
    The docs: $?.

    $? is assigned a value in the background on the line @output = `$cmd`;, independent of what you do. The value itself depends on the status code returned from (I presume) iwodstart.

    If you have multiple commands in $cmd or any i/o redirects (which you do), then the child process is actually a shell around your command set, and the return value in $? is the returned status code from the shell.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1022109]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found