Hi All,

I am currently writing a perl program for linux which needs to run another perl script with a commandline option half way through, 'myscript.pl -h'. If it didn't have the '-h' I would use require. I could use backticks to shell out, but this is slow. Is there a better way to call another perl program with options?

Many thanks in advance,
Ian

Recommended Answers

All 5 Replies

Not that I know of.... You could consider using the open command, and running the script as a thread such as:

open (FH, "yourfile.pl -h");
close(FH);

You could also use the system command, but it would seem to me that shelling no matter how you do it is going to take a bit of processing time.

You can pass command line arguments by changing @ARGV. It is best to make a local copy.

For example,

inc.pl:

#!/usr/bin/perl
use strict; # just for kicks

for my $item (@ARGV) {
	print "$item\n"; # print what we think are our command line args!
}

tmp.pl:

#!/usr/bin/perl
use strict; # just for kicks

{
	local @ARGV;

	@ARGV = (1, 2, 3, 4, "Hello!"); # set our command line args!

	eval { require "inc.pl" };
        # the 'eval' catches the exception that occurs when
        # inc.pl fails to return true (which can also be alleviated
        # by ending "inc.pl" with a true value, such as 1; in a
        # line by itself).
}

print "\nOriginal Arguments:\n\n";

for my $item (@ARGV) {
	print "$item\n";
}

If you run:
perl tmp.pl arg1 arg2 arg3
you'll see the output

1
2
3
4
Hello!

Original Arguments:

arg1
arg2
arg3

You can pass command line arguments by changing @ARGV. It is best to make a local copy.

For example,

inc.pl:

#!/usr/bin/perl
use strict; # just for kicks
 
for my $item (@ARGV) {
    print "$item\n"; # print what we think are our command line args!
}

tmp.pl:

#!/usr/bin/perl
use strict; # just for kicks
 
{
    local @ARGV;
 
    @ARGV = (1, 2, 3, 4, "Hello!"); # set our command line args!
 
    eval { require "inc.pl" };
        # the 'eval' catches the exception that occurs when
        # inc.pl fails to return true (which can also be alleviated
        # by ending "inc.pl" with a true value, such as 1; in a
        # line by itself).
}
 
print "\nOriginal Arguments:\n\n";
 
for my $item (@ARGV) {
    print "$item\n";
}

If you run:
perl tmp.pl arg1 arg2 arg3
you'll see the output

1
2
3
4
Hello!

Original Arguments:

arg1
arg2
arg3

But would this take care of the option with which we need to call the perl script?? I too have the same task and I figure out how to call another script with options?

Did u find a solution?? If yes then how?? How do we call a script with options?

iblair,

Are you sure you need to use the -h option? All it does is list the valid options. What is it you are trying to do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.