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

T

Tony

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
 
T

Tony

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
[...]
 
N

Nicholas Dronen

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
 
B

Bryan Castillo

Tony said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top