Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Problem with "system" call - multiple invocations (Newbie) (http://www.velocityreviews.com/forums/t869652-problem-with-system-call-multiple-invocations-newbie.html)

Tony 06-24-2003 01:20 AM

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 06-24-2003 03:49 AM

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.



Nicholas Dronen 06-24-2003 04:09 AM

Re: Problem with "system" call - multiple invocations (Newbie)
 
Tony <anthony@no_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?" http://www.aglami.com/tpfaq.html
"Meanings are another story." http://www.ifas.org/wa/glossolalia.html

Bryan Castillo 06-24-2003 06:08 AM

Re: Problem with "system" call - multiple invocations (Newbie)
 
"Tony" <anthony@no_spam.movielink.net.au> wrote in message news:<pan.2003.06.24.01.20.28.773936@no_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 parsed 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



All times are GMT. The time now is 12:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.