944,001 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 32765
  • Perl RSS
Jun 27th, 2005
0

calling one perl program from within another

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iblair is offline Offline
1 posts
since Jun 2005
Jun 27th, 2005
0

Re: calling one perl program from within another

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

Perl Syntax (Toggle Plain Text)
  1. open (FH, "yourfile.pl -h");
  2. 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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jun 28th, 2005
0

Re: calling one perl program from within another

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)
  1. #!/usr/bin/perl
  2. use strict; # just for kicks
  3.  
  4. for my $item (@ARGV) {
  5. print "$item\n"; # print what we think are our command line args!
  6. }

tmp.pl:
Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2. use strict; # just for kicks
  3.  
  4. {
  5. local @ARGV;
  6.  
  7. @ARGV = (1, 2, 3, 4, "Hello!"); # set our command line args!
  8.  
  9. eval { require "inc.pl" };
  10. # the 'eval' catches the exception that occurs when
  11. # inc.pl fails to return true (which can also be alleviated
  12. # by ending "inc.pl" with a true value, such as 1; in a
  13. # line by itself).
  14. }
  15.  
  16. print "\nOriginal Arguments:\n\n";
  17.  
  18. for my $item (@ARGV) {
  19. print "$item\n";
  20. }

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

1
2
3
4
Hello!

Original Arguments:

arg1
arg2
arg3
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Oct 27th, 2006
0

Re: calling one perl program from within another

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)
  1. #!/usr/bin/perl
  2. use strict; # just for kicks
  3.  
  4. for my $item (@ARGV) {
  5. print "$item\n"; # print what we think are our command line args!
  6. }

tmp.pl:
Perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2. use strict; # just for kicks
  3.  
  4. {
  5. local @ARGV;
  6.  
  7. @ARGV = (1, 2, 3, 4, "Hello!"); # set our command line args!
  8.  
  9. eval { require "inc.pl" };
  10. # the 'eval' catches the exception that occurs when
  11. # inc.pl fails to return true (which can also be alleviated
  12. # by ending "inc.pl" with a true value, such as 1; in a
  13. # line by itself).
  14. }
  15.  
  16. print "\nOriginal Arguments:\n\n";
  17.  
  18. for my $item (@ARGV) {
  19. print "$item\n";
  20. }

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bucchi is offline Offline
3 posts
since Oct 2006
Oct 27th, 2006
0

Re: calling one perl program from within another

Did u find a solution?? If yes then how?? How do we call a script with options?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bucchi is offline Offline
3 posts
since Oct 2006
Oct 27th, 2006
0

Re: calling one perl program from within another

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?
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: How do i run perl on windows 98
Next Thread in Perl Forum Timeline: Regular expression doesn't make sense





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC