Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Spawning other processes but continue script


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

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

Paul!

I really appreciate your expertise.
Thank you mate.

Simon

"Paul Lalli" <mri@gmail.com> wrote in message

news:1180955598.686168.271650@p77g2000hsh.googlegroups.com...

SimonH wrote:

...

> 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?

Since you are running on Windows, you could use Window's own facility
for doing this:

    system 'start notepad';

I'm not sure, though, what you expect nodepad to return for print() to
print??  Whatever that is, I'm sure you will find the output from
"start" to be less edifying, particularly since the backticks appear to
force print() to wait for the output despite the presence of 'start'
(tested on XP Home sp2 with Activestate Perl 5.8.8 build 819).

...

> Simon

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

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

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

"Joe Smith" <j@inwap.com> wrote in message

news:XYGdnZA3XZxlgPjbnZ2dnUVZ_jednZ2d@comcast.com...

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc