Perl 'DO' command with parameters??

A

ant_howell

Hi

I'm using the Perl 'DO' command to successfully execute a Perl script
from inside an existing Perl script. I'm using the following piece of
code:-

do '<path to script>\\script.pl'

Now, is there a way to use the above but also pass the script being
called, a parameter list? I've tried this several times with no luck.
The $! value is "No such file or directory",

Basically I am using

do '<path to script>\\script.pl param1 param2'

Alternatively, if there is another way to call a script with parameters
from inside another script, except using a system call, then I'm all
ears!

Regards

Ant
 
P

Paul Lalli

I'm using the Perl 'DO' command to successfully execute a Perl script
from inside an existing Perl script. I'm using the following piece of
code:-

do '<path to script>\\script.pl'

Now, is there a way to use the above but also pass the script being
called, a parameter list?

No, because do() does not execute a program. It simply evaluates and
runs the perl code contained in the file you pased to it.
I've tried this several times with no luck.
The $! value is "No such file or directory",

Basically I am using

do '<path to script>\\script.pl param1 param2'

Alternatively, if there is another way to call a script with parameters
from inside another script, except using a system call, then I'm all
ears!

I have no idea if this would work, but you could try:

{
local @ARGV = qw/param1 param2/;
do 'script.pl';
}

I have no idea if it would work because I would never do something like
that, and I'm not about to test it. I would use system(), because
that's what it's meant to do. What is your reasoning for refusing to
use the obvious and correct solution to your problem?

In reality, of course, you shouldn't be using do() at all. Even the
documentation for do() tells you you shouldn't be using it:
perldoc -f do
...
Note that inclusion of library modules is better
done with the "use" and "require" operators, which
also do automatic error checking and raise an
exception if there's a problem.

Paul Lalli
 
A

ant_howell

Hi Paul

Thanks for the info. Your correct. I have misunderstood the Do
function.
I've looked at the System command and it does do what I want but its
not perfect in capturing a return value from the called script. It also
posts an error (maybe this could be surpressed in some way) if the
called script does not exist (which is likely with what I am doing).

Thanks for your attention.

Regards

Ant
 
A

ant_howell

Hi Chris

Thanks for your info. I'll look into your suggestion.
Capturing STDERR is something I will need.

Regards

Ant
 
P

Paul Lalli

Thanks for the info. Your correct. I have misunderstood the Do
function.
I've looked at the System command and it does do what I want but its
not perfect in capturing a return value from the called script.

Yes it is. Either you're using it wrong, or you're using the wrong
definition of "return value". If you actually mean "output", then
please see:

perldoc -q "output.*system"
Found in /opt/perl/lib/5.6.1/pod/perlfaq8.pod
Why can't I get the output of a command with system()?
It also
posts an error (maybe this could be surpressed in some way)

Of course it could, the same way you supress errors when calling any
other program from the shell - by redirecting STDERR.

system ("foobar.pl 2>/dev/null");
if the
called script does not exist (which is likely with what I am doing).

Then why not just check for its existance first?

system ('foobar.pl') if -f 'foobar.pl';

perldoc -f -X

Paul Lalli
 
A

ant_howell

Hi Paul

Thanks again.

Your correct, I can use file existence prior to passing it into system.
In fact, I've done this in other parts of my
script for different things so I don't know why I did not think of this
myself.
As for a return code, yep I am getting something now thanks.

Regards

An
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top