Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Spawning other processes but continue script

Reply
Thread Tools

Spawning other processes but continue script

 
 
SimonH
Guest
Posts: n/a
 
      06-04-2007
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-04-2007
On Jun 4, 5:54 am, "SimonH" <s...@bigpond.net.au> wrote:
> 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

 
Reply With Quote
 
 
 
 
SimonH
Guest
Posts: n/a
 
      06-04-2007
Paul!

I really appreciate your expertise.
Thank you mate.

Simon

"Paul Lalli" <> wrote in message
news: oups.com...
> On Jun 4, 5:54 am, "SimonH" <s...@bigpond.net.au> wrote:
>> 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
>



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-05-2007
SimonH wrote:
> 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
 
Reply With Quote
 
SimonH
Guest
Posts: n/a
 
      06-05-2007
Thanks guys.....Ill do some more reading....still a newbie big time...Thanks
for all your help.

"Joe Smith" <> wrote in message
news:. ..
> SimonH wrote:
>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling processes and what to "feed" other processes Marc Heiler Ruby 1 05-24-2009 05:37 PM
General Solution to spawning a Unix process whose arguments cannotbe seen by other processes ... Xeno Campanoli Ruby 2 11-04-2008 06:43 AM
Spawning daemon processes Thiago Arrais Ruby 4 09-01-2006 08:23 PM
spawning processes from Ruby/TK Mehr, Assaph (Assaph) Ruby 2 02-11-2004 07:33 PM
Spawning child processes Chris C Programming 2 07-30-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57