Spawning other processes but continue script

S

SimonH

Hi guys!

I have the following script which opens a machine.txt file, with format:

machine1
machine2
machine3
etc

====================================================
#!perl
open (INPUT,"machine.txt") or die "cant open machine.txt";
@computers = <INPUT>;
while (<INPUT>) {
chomp;
push @computers, $_;
}
foreach $a (@computers) {
if ($a eq "dell101\n") {
print "hi there simon\n";
print `notepad`;
}
else {
print "you are not my machine\n";
}

#print $a;
#
}
====================================================

What id like help with is the following....

If I comment out the line..
print `notepad`;
Execution proceeds as expected.
When I leave this in, it opens notepad, while the command prompt waits.
Is there any way to spawn other processes, but continue execution of your
original script?

Thanks guys.

Simon
 
P

Paul Lalli

Hi guys!

I have the following script which opens a machine.txt file, with format:

machine1
machine2
machine3
etc

====================================================
#!perl
open (INPUT,"machine.txt") or die "cant open machine.txt";
@computers = <INPUT>;

This statement reads ALL lines of machine.txt into @computers. There
is nothing left to read at this point.
while (<INPUT>) {
chomp;
push @computers, $_;}

This block is therefore a no-op. There is nothing left to read, so
this loop is never executed.
foreach $a (@computers) {

If you're just going to process the data line-by-line anyway, it makes
no sense to bother reading the entire thing into an array. Get rid of
the first statement, keep the second block, and put the following code
into that block. So:

while (my $computer = <INPUT>) {

(I changed your $a to $computer, because $a and $b are "special" in
Perl, and should generally only be used for sort subroutines)

if ($a eq "dell101\n") {
print "hi there simon\n";
print `notepad`;
}
else {
print "you are not my machine\n";
}
Is there any way to spawn other processes, but continue execution
of your original script?

Yes. Fork a new process, and exec the program in the new child
process.

perldoc -f fork
perldoc -f exec

if (fork()) { #parent
do_parent_stuff();
} else { #child
exec 'notepad.exe';
}


Depending on your shell, you might also be able to just put a '&' at
the end of the command you want to run, to tell the shell to run the
process in the background. I have no idea how or if that works in
Windows, however.

Paul Lalli
 
J

Joe Smith

SimonH said:
print `notepad`;

Why do you want to print 0? That is, why are backticks (``) being used?

It looks like the author does not know the difference between

$captured_output_from_program = `notepad`;

and

$error_code_exit_status = system 'notepad';

C:\> perldoc -q "output of a command"

-Joe
 
S

SimonH

Thanks guys.....Ill do some more reading....still a newbie big time...Thanks
for all your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top