How can i make a perl program lauch another perl program that takes options

How can i make a perl program lauch another perl program that takes options

Post by alex.james » Fri, 29 Jun 2007 22:01:10


Hi all,

Any help on this would be appreciated :)

Currently I have a perl program thats required to call another perl
program. The second one takes arguments and can be called as:

perl vplan_all.pl 5.6 24.0 ajtest

Here 5.6, 24.0 and ajtest are all command line options.

I have to call this from a second program that i wrote. I tried the
call as below -

system( "perl vplan_all.pl 5.6 24.0")
or die "Couldn't run the vplan_all.pl script: $!\n";

However, this doesn't seem to work. The shell reports that perl can't
find the file. However, when i simply try with -

system( "perl vplan_all.pl")
or die "Couldn't run the vplan_all.pl script: $!\n";

The script is now called and i get the result -

vp_all: USAGE: perl vplan_all.pl <Version> <Build>
<File_name_to_be_created>

which is the expected behaviour when no options are provided.
So apparently the shell calls perl and passes the entire "perl
vplan_all.pl 5.6 24.0" as a chunk. Perl can't then seem to
differentiate between the code name and the options.

I've tried bypassing the shell [ system(xxx,xxx,xx)] but that doesn't
seem to help.

Please do point out any ideas on this one.
Thx in advance!
Alex Jamestin
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by iaccount » Sat, 30 Jun 2007 21:39:55


If vplan_all.pl is in fact it's own application, try calling it without
calling Perl first:

system("/path/to/program/vp_all.pl arg1 arg2") or die "blah blah: $!";

BTW, AFAIR, inserting a \n after the $! will negate the $!, but I may be
mistaken.

Steve

 
 
 

How can i make a perl program lauch another perl program that takes options

Post by chas.owen » Sat, 30 Jun 2007 22:04:17


snip
snip

Hmm, what OS are you using? And by system(xxx,xxx,xx) do you mean

system("perl vplan_all.pl", "5.6", "24.0") == 0
or die "blah";

or

system( "perl", "vplan_all.pl", "5.6", "24.0") == 0
or die "blah";
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by alex.james » Sat, 30 Jun 2007 22:10:59

Well, i tried both the options -


I think when perl -> shell -> perl . Here perl doesn't seem to
understand that vplan_all.pl is the script name and 5.6 and 24.0 are
options to be passed.

The exact same text ie. perl vplan_all.pl 5.6 21.0 works fine when i
just run it from the command line. Confusing! :\
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by chas.owen » Sat, 30 Jun 2007 23:02:04


I have a FreeBSD box handy, but I cannot reproduce your error. Do the
two scripts below work for you?

/home/cowens>uname -smr
FreeBSD 4.11-STABLE i386
/home/cowens>cat t.pl
#!/usr/bin/perl

system("perl f.pl foo bar baz") == 0
or die "perl blew up: $!";
/home/cowens>cat f.pl
#!/usr/bin/perl -l

print map { "[$_] " } @ARGV;
/home/cowens>perl t.pl
[foo] [bar] [baz]
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by alex.james » Sat, 30 Jun 2007 23:17:47

Thanks Chas,
This does work. So there's no problem with perl itself. I guess now,
i'll have to take a look at vplan_all.pl.

The sucky thing is that road is too much of pain to go down. (Though
now i don't think ill be able to sleep without thinking about this :))
Oh well, Bug begets bug :)

Anyways,
thx again for helping out
Alex
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by chas.owen » Sat, 30 Jun 2007 23:23:35


I don't think the problem is in vplan_all.pl. I looked back at your
original code and noticed a bug:
system( "perl vplan_all.pl 5.6 24.0")
or die "Couldn't run the vplan_all.pl script: $!\n";

This should be

system( "perl vplan_all.pl 5.6 24.0") == 0
or die "Couldn't run the vplan_all.pl script: $!\n";
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by alex.james » Sat, 30 Jun 2007 23:34:52

Okay,
system returns a 0 if it executes successfully. So then, die is called
only if system returns anything other than a 0.

And in the old code system would return a 0 after calling perl. This
would short-circuit and call die.

Makes sense :) Will try out and reply.

Thx,
Alex
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by alex.james » Sun, 01 Jul 2007 00:00:02

Tried that but it still won't work. Doh! Stupid bug :\

I'd run this test script below -

#! /usr/bin/perl
use strict;
use warnings;

my $vplan_parent_path = "\/pdd\/qes\/vsuites\/vplan";


system("perl $vplan_parent_path\/vplan_all.pl") == 0
or die "perl blew up: $!";


And the o/p got is -

vp_all: USAGE: perl vplan_all.pl <Version> <Build> <File_name_to_be_created>

perl blew up: No such file or directory at test2.pl line 8.


Interesting thing is, while perl locates the second perl prog
successfully - it still calls die after that.


Anyways, gotta leave for friday (I'm on the other side of the world :))
Thx,
Alex
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by chas.owen » Sun, 01 Jul 2007 00:57:49


snip
snip

You don't need to escape those slashes.

snip
snip

Same thing here.

snip
snip

You said before that it was supposed to die if it got no arguments, so
that is the expected behaviour. As to why it keeps saying "No such
file or directory", well, if I remember correctly $! is the system
call errno, not the return from system. I think you want $?
(specifically $? >> 8). Here is what I think you want

system("perl vplan_all.pl 5.6 24.0 ajtest") == 0
or die "vplan_all failed with an exit code of " . ($? >> 8);
 
 
 

How can i make a perl program lauch another perl program that takes options

Post by tom » Sun, 01 Jul 2007 08:01:36


Could it be that you have a different $PATH, a different directory or
other envronment changes? If your perl binary is properly built and
installed, it should be capable of running another perl.

system("perl", "-lwe", "print q(perl works!)") == 0
or die "Can't call perl: $!";

Cheers!

--Tom Phoenix
Stonehenge Perl Training