in reply to Problem with system ()

There are three problems with your code :

First, system() returns 0 on success and non-zero on failure, so the method to use it is :

system "..." and die "Error: $! / $?";

The cd thing you enter on the command line is no external command but a built-in command of cmd.exe. Thus you would have to do system($ENV{COMSPEC},'/c cd c:\temp').

Using shell commands to set the current directory is not guaranteed to influence the current working directory of the parent. You want to use the CwdFile::chdir module to change the directory and the unlink() function to erase files.

Update: Changed the module name
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Replies are listed 'Best First'.
Re: Re: Problem with system ()
by MarkM (Curate) on Mar 19, 2003 at 02:55 UTC

    A few corrections:

    1. 'system() and die()' is not very easy to parse. I strongly suggest "system()==0 or die" or even "if (system()!=0) { die }".
    2. system() does invoke cmd.exe on WINNT, just as it invokes command.com on WIN9x and /bin/sh on UNIX. There is no need to $ENV{'COMSPEC'}.
    3. You say "using shell commands to set the current directory is not guaranteed to influence the current working directory of the parent." In fact, chdir() system calls in a sub-process do not affect the current working directory of the parent at all on UNIX or WIN32. You really want to use chdir() directly, as "chdir($dir) or die".