| | |
calling one perl program from within another
![]() |
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Solved Threads: 0
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
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
Not that I know of.... You could consider using the open command, and running the script as a thread such as:
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.
Perl Syntax (Toggle Plain Text)
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:
tmp.pl:
If you run:
perl tmp.pl arg1 arg2 arg3
you'll see the output
1
2
3
4
Hello!
Original Arguments:
arg1
arg2
arg3
For example,
inc.pl:
Perl Syntax (Toggle Plain Text)
#!/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:
Perl Syntax (Toggle Plain Text)
#!/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
•
•
Join Date: Oct 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
You can pass command line arguments by changing @ARGV. It is best to make a local copy.
For example,
inc.pl:
Perl Syntax (Toggle Plain Text)
#!/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:
Perl Syntax (Toggle Plain Text)
#!/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?
![]() |
Similar Threads
- passing parameters from a perl program to a c++ .exe file (Perl)
- Call Perl program from sed. (Legacy and Other Languages)
- How to invoke a perl program from a perl program. (Perl)
- Using passwd in a perl file to automate user creation (Perl)
- Executing a C++ program in another (C++)
Other Threads in the Perl Forum
- Previous Thread: How do i run perl on windows 98
- Next Thread: Regular expression doesn't make sense
| Thread Tools | Search this Thread |






