1

I have met a problem when I tried to execute a perl script within my perl script. This is a small part of a larger project that I'm working on.

Below is my perl script code:

use strict;
use warnings;
use FindBin qw($Bin);

#There are more options, but I just have one here for short example
print "Please enter template file name: "
my $template = <>;
chomp($template);

#Call another perl script which take in arguments
system($^X, "$Bin/GetResults.pl", "-templatefile $template");

the "GetResults.pl" takes in multiple arguments, I just provide one here for example. Basically, if I was to use the GetResults.pl script alone, in the command line I would type:

perl GetResults.pl -templatefile template.xml

I met two problems with the system function call above. First of all, it seems to remove the dash in front of my argument when I run my perl script resulting in invalid argument error in GetResults.pl.

Then I tried this

system($^X, "$Bin/GetResults.pl", "/\-/templatefile $template");

It seems OK since it does not complain about earlier problem, but now it says it could not find the template.xml although I have that file in the same location as my perl script as well as GetResults.pl script. If I just run GetResults.pl script alone, it works fine.

I'm wondering if there is some issue with the string comparison when I use variable $template and the real file name located on my PC (I'm using Window 7).

I'm new to Perl and hope that someone could help. Thank you in advance.

1 Answer 1

5

Pass the arguments as an array, just as you would with any other program (a Perl script is not special; that it is a Perl script is an implementation detail):

system($^X, "$Bin/GetResults.pl", "-templatefile", "$template");

You could line everything up in an array and use that, too:

my @args = ("$Bin/GetResults.pl", "-templatefile", "$template");
system($^X, @args);

Or even add $^X to @args. Etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Could you please also add the return from the called script to capture the return status?
I'm not sure what you mean. You can capture the return value from system by my $rv = system(…); which is trivial and obvious and … what's the problem you're after help with? perldoc -f system may assist you.
The perl script "GetResults.pl" may be returning something after completion. I would like to capture that return. So, the $rv will contain the return value of that perl script?
Yes — see perldoc -f system for guidelines on how to interpret the return value from a system statement/call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.