in reply to system() command
I personally would always prefer assigning a lexical var for the return code instead of using some "magic" Perl variable ($!). my $rc = system($cmd); #use $rc and perhaps $! also, but $rc is "the error code from system"
Ysth is completely correct - get the Perl process to change its directory, do not use a shell. That shell is a separate process.
But a main point here is that when things go "bad", print more stuff, like the return code from system(). Then you see that 256 != -1, etc.
Update: I would also add that most of the time an O/S error code is "0", zero upon success and non-zero when it fails. As with all things with software, this is not true always, but it is often true. So instead of checking for a comparison with -1 (or 256), better is usually is, if ($x !=0) for the error condition.#!usr/bin/perl -w use strict; my $cmd = "cd /cat/bat"; my $rc = system($cmd); print "rc = $rc\n"; #PRINTS: #The system cannot find the path specified. #rc = 256 $cmd = 'cd C:\temp'; $rc = system($cmd); print "rc = $rc\n"; #PRINTS: #rc = 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: system() command
by cdarke (Prior) on Jul 13, 2009 at 08:09 UTC | |
by Marshall (Canon) on Jul 13, 2009 at 11:37 UTC |