in reply to How to get output from a spawned cmd prompt?

First, Windows can't fork processes. It spawns new processes.

The problem is that your error checking is buggy. You're reporting an error when none occurred.

system($file1); if ($? == -1) { die("Can't execute command: $!\n"); } elsif ($? & 127) { die("Command died from signal ", ($? & 127), "\n"); } elsif ($? >> 8) { die("Command exited with error ", ($? >> 8), "\n"); }

Update: While it's true that your error handling is buggy, it's not the cause of your problem. Could you please add the following lines after system (in either in your version or mine) and post the output.

use Devel::Peek; Dump($?);

I have a hard time believeing $? has a non-integer value or an integer value less than -1, but that's the only way you could get the results you report. This command will report quite precisely the value of $?.

Replies are listed 'Best First'.
Re^2: How to get output from a forked cmd prompt?
by Xhings (Acolyte) on Jan 21, 2010 at 09:18 UTC
    Follwoing is the o/p:
    SV = PVMG(0x9b2e8c) at 0x9b9d3c REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x9c4f5c MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x9b9d2c MG_LEN = 1 MG_PTR = 0x9c4f8c "?"

      Me bad. That should have been

      use Devel::Peek; 1 if $?; # mg_get Dump($?);

      But don't bother. The only way only what you said could happen is if system and $? are not returning the same thing like they are suppose to. I could ask you to show me a dump of both to confirm that or to show that what you said is wrong. Neither outcome would be productive.

      Since there's no reason to check both, pick one. (See snippets below.) If you then have problems with it containing the wrong value, we'll investigate why it has the wrong value then, knowing what value is obtained and what the value should be.

      system($cmd); if ($? == -1) { die("Can't execute command: $!\n"); } elsif ($? & 127) { die("Command died from signal ", ($? & 127), "\n"); } elsif ($? >> 8) { die("Command exited with error ", ($? >> 8), "\n"); }
      or
      my $rv = system($cmd); if ($rv == -1) { die("Can't execute command: $!\n"); } elsif ($rv & 127) { die("Command died from signal ", ($rv & 127), "\n"); } elsif ($rv >> 8) { die("Command exited with error ", ($rv >> 8), "\n"); }

      By the way, make sure you aren't using system and fork in the same program.