954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

calling one perl program from within another

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

iblair
Newbie Poster
1 post since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

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 output1
2
3
4
Hello!

Original Arguments:

arg1
arg2
arg3

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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 output1 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?

Bucchi
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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

Bucchi
Newbie Poster
3 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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?

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You