calling one perl program from within another

Reply

Join Date: Jun 2005
Posts: 1
Reputation: iblair is an unknown quantity at this point 
Solved Threads: 0
iblair iblair is offline Offline
Newbie Poster

calling one perl program from within another

 
0
  #1
Jun 27th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: calling one perl program from within another

 
0
  #2
Jun 27th, 2005
Not that I know of.... You could consider using the open command, and running the script as a thread such as:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: calling one perl program from within another

 
0
  #3
Jun 28th, 2005
You can pass command line arguments by changing @ARGV. It is best to make a local copy.

For example,

inc.pl:
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: Bucchi is an unknown quantity at this point 
Solved Threads: 0
Bucchi Bucchi is offline Offline
Newbie Poster

Re: calling one perl program from within another

 
0
  #4
Oct 27th, 2006
Originally Posted by Rashakil Fol View Post
You can pass command line arguments by changing @ARGV. It is best to make a local copy.

For example,

inc.pl:
  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 3
Reputation: Bucchi is an unknown quantity at this point 
Solved Threads: 0
Bucchi Bucchi is offline Offline
Newbie Poster

Re: calling one perl program from within another

 
0
  #5
Oct 27th, 2006
Did u find a solution?? If yes then how?? How do we call a script with options?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: calling one perl program from within another

 
0
  #6
Oct 27th, 2006
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC