Run.pm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. #-----------------------------------------------------------------------------
  5. ## @file Run.pm
  6. # @brief A module for running perl scripts in threaded environment
  7. # @see Run.pm
  8. ## @class Run
  9. # Package
  10. package Run;
  11. # Globals
  12. my $DEBUG = 0;
  13. #----------------------------------------------------------------------
  14. ## @fn runAndWar(@)
  15. # @brief Run and warn
  16. # @param args array
  17. # @return 1 is successful, 0 if not
  18. sub runAndWarn(@) {
  19. my (@args) = (@_);
  20. if ($DEBUG) {
  21. print STDERR "Run::runAndWarn()\n";
  22. }
  23. # Run
  24. my $res = Run::run(@args);
  25. # Die on any error
  26. if ($res == -1) {
  27. warn('Unable to execute program: ' . join(' ', @args) . ': ' . $! . "\n");
  28. return 0;
  29. } elsif ($res & 127) {
  30. warn('Child exited on signal: ' . join(' ', @args) . ': ' . ($res & 127) . "\n");
  31. return 0;
  32. } elsif ($res != 0) {
  33. warn('Child exited with non-zero value: ' . join(' ', @args) . ': ' . ($res >> 8) . "\n");
  34. return 0;
  35. }
  36. # If we get here, all is well
  37. return 1;
  38. }
  39. #----------------------------------------------------------------------
  40. ## @fn runAndCheck(@)
  41. # @brief Run and check
  42. # @param args
  43. # @return 1 if successful, 0 if not
  44. sub runAndCheck(@) {
  45. my (@args) = (@_);
  46. if ($DEBUG) {
  47. print STDERR "Run::runAndCheck()\n";
  48. }
  49. # Run
  50. my $res = Run::run(@args);
  51. # Die on any error
  52. if ($res == -1) {
  53. die('Unable to execute program: ' . join(' ', @args) . ': ' . $! . "\n");
  54. } elsif ($res & 127) {
  55. die('Child exited on signal: ' . join(' ', @args) . ': ' . ($res & 127) . "\n");
  56. } elsif ($res != 0) {
  57. die('Child exited with non-zero value: ' . join(' ', @args) . ': ' . ($res >> 8) . "\n");
  58. }
  59. # If we get here, all is well
  60. return 1;
  61. }
  62. #----------------------------------------------------------------------
  63. ## @fn run(@)
  64. # @brief Run
  65. # @param args arry
  66. # @return results
  67. sub run(@) {
  68. my (@args) = (@_);
  69. if ($DEBUG) {
  70. print STDERR 'Run::run: ' . join(' ', @args) . "\n";
  71. }
  72. # Run
  73. system { $args[0] } @args;
  74. # Return the result
  75. return $?;
  76. }
  77. # Packages must return true
  78. return 1;