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

I have been running a program that assembles parameters for an executable program and ends by saying

exec $command;

where $command is the executable (it happens to be a secure FTP client) followed by some options.

I can run my $command manually in a command window, with the same outcome as when perl gives it to exec.

But now I want to start checking that FTP has actually finished downloading the files I've requested, so I've tried

system $command;

But not only does my program not get back control to run my checks, the command actually fails to run:

Why such a big difference between what I can do with exec and with system?

(I run Perl 5.8 on Windows Server 2003.)

Update: FWIW, $command is actually ftpscrpt -w $password -f $script_file, where ftpscrpt is the script utility of a proprietary secure FTP client. I originally left this vague because I didn't think it would be of interest. Whatever it is, it works with exec, fails with system. Isn't that strange?

Have found a solution but am still puzzled

Inspired by the anonymous response below, I started tweaking things. Looked in the program directory, changed ftpscrpt to ftpscrpt.exe with no luck, looked again at the directory and changed to ftpscrpt.com and it worked the way perlfunc had originally led me to believe it should.

My more general question remains - how do I reconcile my experience with what I read in system, namely

system LIST

Does exactly the same thing as "exec LIST", except that a fork is done first, and the parent process waits for the child process to complete.

Update: My thanks to rovf and salva for their excellent suggestions, which I look forward to trying out in my spare time. The lesson I'll take to heart is that my perl is not identical with the Perl of the manpages, and IPC is one domain where the differences need watching.

  • Comment on [Have solution now but am still puzzled] Why can't I give system the same command I've been giving to exec?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Why can't I give system the same command I've been giving to exec?
by Anonymous Monk on Nov 18, 2008 at 08:05 UTC
    The problem is with $command. Try
    system 'cmd.exe /c', $command;
    If that doesn't work you'll have to debug $command.
Re: Why can't I give system the same command I've been giving to exec?
by massa (Hermit) on Nov 18, 2008 at 13:29 UTC
    Another thing you can do is substitute the ftpscrpt call by your own Net::FTP-based perl script... (it's a core module!)
    []s, HTH, Massa (κς,πμ,πλ)

      Sounds like fun, but not for me at work, alas. We have a mandate to use a given FTP client.

Re: Why can't I give system the same command I've been giving to exec?
by Bloodnok (Vicar) on Nov 18, 2008 at 13:18 UTC
    Are you sure that perl is doing the variable expansion ? If you' passing the string 'ftpscrpt -w $password -f $script_file' to the shell (or what passes for one in Windoze) via system(), then the 'shell' won't understand the $ variable names - since it DOS uses %<var>% to denote environment variables.

    It might also pay to have a look round Dave Roths' website (www.roth.net) - invariably most helpful when Windoze problems are encountered...

    .oO(I wonder if it might possibly be something to do with ftpscrpt requiring a controlling terminal to be attached? - Ah well, that's thinx bubbles for you;-))

    A user level that continues to overstate my experience :-))
      Are you sure that perl is doing the variable expansion ?

      Yes, I print my command before passing it to exec, and get confirmation both from the printout and from the log file that's generated when the command runs under exec.

      Thanks for the Roth link.

      Thanks especially for the thought about the controlling terminal. I don't exactly know how to act on that thought, but this sounds like the kind of thing that might vary between exec and system.

Re: Why can't I give system the same command I've been giving to exec?
by rovf (Priest) on Nov 18, 2008 at 15:16 UTC

    I know it sounds a bit like black magic, but: I would (just for debugging purpose) create a temporary BAT file, write the ftpscrpt command into this file, and then execute the BAT file via system. The outcome should be the same. If you still get the error, does the error also occur when you call the batch file from the command line? If it does, it should be now trivial to track down; otherwise, it might be worth checking the environment (including COMSPEC).

    BTW, how is the effect if you simply do

    perl -we "system(qq(ftpscrpt ...))"
    from the command line?

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: [Have solution now but am still puzzled] Why can't I give system the same command I've been giving to exec?
by salva (Canon) on Nov 18, 2008 at 15:22 UTC
    Under Windows, you can use Process Monitor to watch the OS calls performed by some program under the hood. Using it, you should be able to see what system does different that causes your script to fail.

    Take a look at Proc::Background also.

    Regarding your latest query, system documentation talks about the Unix port of Perl. Windows OSs do not support fork at all (and even exec is probably just emulated there).