Spawning other processes but continue script

Discussion in 'Perl Misc' started by SimonH, Jun 4, 2007.

  1. SimonH

    SimonH Guest

    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
    SimonH, Jun 4, 2007
    #1
    1. Advertising

  2. SimonH

    Paul Lalli Guest

    On Jun 4, 5:54 am, "SimonH" <> 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 Lalli, Jun 4, 2007
    #2
    1. Advertising

  3. SimonH

    SimonH Guest

    Paul!

    I really appreciate your expertise.
    Thank you mate.

    Simon

    "Paul Lalli" <> wrote in message
    news:...
    > On Jun 4, 5:54 am, "SimonH" <> 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
    >
    SimonH, Jun 4, 2007
    #3
  4. SimonH

    Joe Smith Guest

    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
    Joe Smith, Jun 5, 2007
    #4
  5. SimonH

    SimonH Guest

    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
    SimonH, Jun 5, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Spawning child processes

    Chris, Jul 29, 2003, in forum: C Programming
    Replies:
    2
    Views:
    351
    Chris
    Jul 30, 2003
  2. Mehr, Assaph (Assaph)

    spawning processes from Ruby/TK

    Mehr, Assaph (Assaph), Feb 10, 2004, in forum: Ruby
    Replies:
    2
    Views:
    113
    Vadim Konovalov
    Feb 11, 2004
  3. Thiago Arrais

    Spawning daemon processes

    Thiago Arrais, Aug 31, 2006, in forum: Ruby
    Replies:
    4
    Views:
    168
    Thiago Arrais
    Sep 1, 2006
  4. Xeno Campanoli
    Replies:
    2
    Views:
    93
    Heesob Park
    Nov 4, 2008
  5. Marc Heiler
    Replies:
    1
    Views:
    171
    Robert Klemme
    May 24, 2009
Loading...

Share This Page