Professional Web Applications Themes

Problem with "system" call - multiple invocations (Newbie) - PERL Miscellaneous

Newbie question, Hi guys, I need to invoke a perl script from another perlscript with different parms passed to the second perl script. Code snippet ============== my ($no1,$no2) = (0,1) my $num = "single digit var from other part of program"; my $file = "/some/path/file_" my $perl_script = "/some/path/perl_program"; if ($num == 1) { system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once exit(0); if ($num == 2) { system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times system "$perl_script" , "$file$no2" , "&"; # with different file name. exit(0); # My "$perl_script" program takes the # ...

  1. #1

    Default Problem with "system" call - multiple invocations (Newbie)


    Newbie question,

    Hi guys,

    I need to invoke a perl script from another perlscript
    with different parms passed to the second perl script.

    Code snippet
    ==============
    my ($no1,$no2) = (0,1)
    my $num = "single digit var from other part of program";
    my $file = "/some/path/file_"
    my $perl_script = "/some/path/perl_program";

    if ($num == 1) {
    system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    exit(0);
    if ($num == 2) {
    system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    system "$perl_script" , "$file$no2" , "&"; # with different file name.
    exit(0);

    # My "$perl_script" program takes the
    # file name and processes it in a process that never
    # will return, that is why I use "&" so the invoking
    # perl script can start the next invocation when there are
    # 2 different file names.

    The above does not work as expected. With $num = 2 the first
    invocation of "$perl_script" runs but not the second one. Killing
    the first one using kill PID allows the second one to start, in other
    words the "&" does not seem to do what it is supposed to do, let
    the calling script continue with the next item on the list.

    What am I doing wrong here?

    I have tried various combinations of `` '' "" but nothing gives.

    Tony

    --
    --------------------------------------------------------------
    To reply directly send to: anthony AT movielink DOT net DOT au
    Replace AT and DOT with and . and mail will get through.

    Tony Guest

  2. #2

    Default Re: Problem with "system" call - multiple invocations (Newbie)

    On Tue, 24 Jun 2003 11:20:28 +1000, Tony wrote:
    >
    > Newbie question,
    >
    Replying to my own post...:-))
    > Hi guys,
    >
    > I need to invoke a perl script from another perlscript
    > with different parms passed to the second perl script.
    >
    > Code snippet
    > ==============
    > my ($no1,$no2) = (0,1)
    > my $num = "single digit var from other part of program";
    > my $file = "/some/path/file_"
    > my $perl_script = "/some/path/perl_program";
    >
    > if ($num == 1) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    > exit(0);
    > if ($num == 2) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    > system "$perl_script" , "$file$no2" , "&"; # with different file name.
    > exit(0);
    After searching even more "igh and low" it would seem that what
    I was trying is simply not possible!

    Instead I inserted fork statements before every required
    invocation of the $perl_script with an "exec" call in every
    child process while placing an exit in the last parent process.

    That seems to have acheived what I wanted, not pretty but works...

    Any other coments on this approach?

    Tny
    >
    > # My "$perl_script" program takes the
    > # file name and processes it in a process that never
    > # will return, that is why I use "&" so the invoking
    > # perl script can start the next invocation when there are
    > # 2 different file names.
    >
    > The above does not work as expected. With $num = 2 the first
    [...]

    --
    --------------------------------------------------------------
    To reply directly send to: anthony AT movielink DOT net DOT au
    Replace AT and DOT with and . and mail will get through.


    Tony Guest

  3. #3

    Default Re: Problem with "system" call - multiple invocations (Newbie)

    Tony <anthonyno_spam.movielink.net.au> wrote:

    T> Newbie question,

    T> Hi guys,

    T> I need to invoke a perl script from another perlscript
    T> with different parms passed to the second perl script.

    T> Code snippet
    T> ==============
    T> my ($no1,$no2) = (0,1)
    T> my $num = "single digit var from other part of program";
    T> my $file = "/some/path/file_"
    T> my $perl_script = "/some/path/perl_program";

    T> if ($num == 1) {
    T> system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    T> exit(0);
    T> if ($num == 2) {
    T> system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    T> system "$perl_script" , "$file$no2" , "&"; # with different file name.
    T> exit(0);

    The following does what you want. See below for an explanation.

    #!/usr/bin/perl
    $|++;
    use strict;
    use warnings;
    my $program = "someprogram";
    my $file = "somefile";
    my $num = 2;
    for (1..$num) {
    system "$program $file$_ &";
    }
    exit(0);

    T> # My "$perl_script" program takes the
    T> # file name and processes it in a process that never
    T> # will return, that is why I use "&" so the invoking
    T> # perl script can start the next invocation when there are
    T> # 2 different file names.

    T> The above does not work as expected. With $num = 2 the first
    T> invocation of "$perl_script" runs but not the second one. Killing
    T> the first one using kill PID allows the second one to start, in other
    T> words the "&" does not seem to do what it is supposed to do, let
    T> the calling script continue with the next item on the list.

    T> What am I doing wrong here?

    See perldoc -f system:

    If there is more than one
    argument in LIST, or if LIST is an array with more
    than one value, starts the program given by the
    first element of the list with arguments given by
    the rest of the list. If there is only one scalar
    argument, the argument is checked for shell
    metacharacters, and if there are any, the entire
    argument is passed to the system's command shell
    for parsing (this is "/bin/sh -c" on Unix plat-
    forms, but varies on other platforms).

    In other words, your invocation of system() with more than one
    argument results in shell metacharacters, such as the ampersand,
    being passed directly as an argument to the program. Very loosely
    speaking, it's the difference between:

    $ ls '&'

    and

    $ ls &

    Regards,

    Nicholas

    --
    "Why shouldn't I top-post?" [url]http://www.aglami.com/tpfaq.html[/url]
    "Meanings are another story." [url]http://www.ifas.org/wa/glossolalia.html[/url]
    Nicholas Dronen Guest

  4. #4

    Default Re: Problem with "system" call - multiple invocations (Newbie)

    "Tony" <anthonyno_spam.movielink.net.au> wrote in message news:<pan.2003.06.24.01.20.28.773936no_spam.movie link.net.au>...
    > Newbie question,
    >
    > Hi guys,
    >
    > I need to invoke a perl script from another perlscript
    > with different parms passed to the second perl script.
    >
    > Code snippet
    > ==============
    > my ($no1,$no2) = (0,1)
    > my $num = "single digit var from other part of program";
    > my $file = "/some/path/file_"
    > my $perl_script = "/some/path/perl_program";
    >
    > if ($num == 1) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    > exit(0);
    > if ($num == 2) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    system "$perl_script $file$no1 &"; # (See below)

    > system "$perl_script" , "$file$no2" , "&"; # with different file name.
    system "$perl_script $file$no2 &"; # (See below)
    > exit(0);
    >
    > # My "$perl_script" program takes the
    > # file name and processes it in a process that never
    > # will return, that is why I use "&" so the invoking
    > # perl script can start the next invocation when there are
    > # 2 different file names.
    >
    > The above does not work as expected. With $num = 2 the first
    > invocation of "$perl_script" runs but not the second one. Killing
    > the first one using kill PID allows the second one to start, in other
    > words the "&" does not seem to do what it is supposed to do, let
    > the calling script continue with the next item on the list.
    Read the docs for "system". If you had called it with one string as a command
    line, it would have been pd for shell meta-characters. The & symbol is
    something understood by the shell, it doesn't really mean anything to perl.
    I would bet that $ARGV[2] would be equal to '&' in your 2nd script.


    >
    > What am I doing wrong here?
    >
    > I have tried various combinations of `` '' "" but nothing gives.
    >
    > Tony
    Bryan Castillo Guest

Similar Threads

  1. "Can't call method "disconnect" on an undefined value..."
    By Metalbomer in forum PERL Modules
    Replies: 5
    Last Post: March 6th, 10:05 PM
  2. How to call the operation-system "Save As" DialogueBox
    By fighttodeath in forum Macromedia Flex General Discussion
    Replies: 7
    Last Post: June 2nd, 07:35 PM
  3. Replies: 3
    Last Post: September 22nd, 11:12 AM
  4. Killing frozen system("something.exe") call - how?
    By Alexandre Bougakov in forum PERL Miscellaneous
    Replies: 0
    Last Post: August 22nd, 07:15 AM
  5. Problem With System Events Scripting--Can't "click at"
    By Aaron Davies in forum Mac Programming
    Replies: 3
    Last Post: July 12th, 10:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139