Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to skip to next Command line

by l.frankline (Hermit)
on Jan 25, 2006 at 08:17 UTC ( [id://525378]=perlquestion: print w/replies, xml ) Need Help??

l.frankline has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a problem only with these below two lines:

system("c:\block.exe");
print "processing second line...";

Please let me describe the problem:

The perl script is working fine, but when the "block.exe" is executed, the perl script is not allowing to execute the next command line, It executes the second line only after the termination of "block.exe".
My question is - How can I execute both the command lines simultaneously.?

Suggestions Please. Thank you!

regards,
Franklin

Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re: How to skip to next Command line
by Corion (Patriarch) on Jan 25, 2006 at 08:49 UTC

    Another method is to use the start command of cmd.exe:

    system("start c:\\block.exe") == 0 or die "Couldn't launch 'c:\\block.exe': $! / $?"; print "processing second line...\n";
Re: How to skip to next Command line
by davido (Cardinal) on Jan 25, 2006 at 08:46 UTC

    system waits for its external command to complete. If you want to continue executing the script oblivious of the progress of the system call, you will have to fork off your system call. Here's a cheap and dirty example:

    if( fork() == 0 ) { system( "C:\\block.exe" ) or die "Command failed: $!\n"; exit; } print "processing second line...\n";

    It's important to keep track of who is the parent and who is the child. fork() returns '0' to the child process, and the child process's ID to the parent. So the if(){} test I've used detects whether a given process is the child (in which case it executes the system command and then exits), or the parent (in which case it continues on to the next line). I could have used exec instead of system, but wanted to provide a way to gracefully handle external command failure. See fork, perlfork, and in a broader sense, perlipc for additional info and discussion of some of the fork pitfalls and handling processes.


    Dave

      The problem with that solution is that if the fork fails, block.exe will be executed, but the rest of the program won't. Also, system returns false if the command was succesful, and any error status is in $?, not $!.

      A better way would be:

      my $pid = fork; die "Fork failed: $!" unless defined $pid; unless ($pid) { system "C:\\block.exe"; die "Command failed with exit code ", $? >> 8 if $?; exit; } print "processing second line...\n";
      Or, if you don't want to handle failures of block.exe in your program:
      my $pid = fork; die "Fork failed: $!" unless defined $pid; unless ($pid) { exec "C:\\block.exe"; die "exec failed: $!"; } print "processing second line...\n";
      Perl --((8:>*
Re: How to skip to next Command line
by jbrugger (Parson) on Jan 25, 2006 at 08:28 UTC
    try to start multiple threads.
    Then you can 'execute' it simultaneously.

    Or use fork, as stated before

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How to skip to next Command line
by Mandrake (Chaplain) on Jan 25, 2006 at 08:44 UTC
Re: How to skip to next Command line
by Anonymous Monk on Jan 25, 2006 at 15:50 UTC
    Please note the asker is on Win32. So, similar to what's been written:

    use Win32; use Win32::Process; Win32::Process::Create($ProcessObj, "C:\\block.exe", "block", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); # As needed: $ProcessObj->Suspend(); $ProcessObj->Resume(); $ProcessObj->Wait(INFINITE);
    Also, a quick cheat:

    open PROG, "c:\\block.exe" or die $!; ...Do Stuff... close PROG; __END__

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2024-04-18 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found