code_junkie

How do I run a Perl script from within a Perl script?

Nov 12th, 2011
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use IPC::System::Simple qw(system capture);
  4.  
  5. # Run a command, wait until it finishes, and make sure it works.
  6. # Output from this program goes directly to STDOUT, and it can take input
  7. # from your STDIN if required.
  8. system($^X, "yourscript.pl", @ARGS);
  9.  
  10. # Run a command, wait until it finishes, and make sure it works.
  11. # The output of this command is captured into $results.
  12. my $results = capture($^X, "yourscript.pl", @ARGS);
  13.  
  14. # Both of these commands allow an exit value of 0, 1 or 2 to be considered
  15. # a successful execution of the command.
  16.  
  17. system( [0,1,2], $^X, "yourscript.pl", @ARGS );
  18. # OR
  19. capture( [0,1,2, $^X, "yourscript.pl", @ARGS );
  20.  
  21. #!/usr/bin/perl
  22. open PERLFILE, "<somePerlScript.pl";
  23. undef $/; # this allows me to slurp the file, ignoring newlines
  24. my $program = <PERLFILE>;
  25. eval $program;
  26.  
  27. {
  28. local @ARGV = qw<param1 param2 param3>;
  29. do '/home/buddy/myscript.pl';
  30. }
  31.  
  32. {
  33. local @ARGV = qw<param1 param2 param3>;
  34. do '/home/buddy/myscript.pl';
  35. }
  36.  
  37. #!/usr/bin/perl
  38. use strict;
  39.  
  40. open(OUTPUT, "date|") or die "Failed to create process: $!n";
  41.  
  42. while (<OUTPUT>)
  43. {
  44. print;
  45. }
  46.  
  47. close(OUTPUT);
  48.  
  49. print "Process exited with value " . ($? >> 8) . "n";
  50.  
  51. # On Unix systems, either of these will execute and just carry-on
  52. # You can't collect output that way
  53. `myscript.pl &`;
  54. system ('myscript.pl &');
  55.  
  56. # On Windows systems the equivalent would be
  57. `start myscript.pl`;
  58. system ('start myscript.pl');
  59.  
  60. # If you just want to execute another script and terminate the current one
  61. exec ('myscript.pl');
Add Comment
Please, Sign In to add comment