param3.pl 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env perl
  2. # Perl parameter example with Getopt::Long, with stdin piping support.
  3. # License: CC0
  4. # Usage:
  5. # ./param3.pl 'some test text'
  6. # echo 'some test' | ./param3.pl -
  7. # To handle CLI parameters
  8. use Getopt::Long;
  9. # Shows up when --help or -h is passed
  10. sub help_text {
  11. print("usage: param3.pl [-h] [TEXT]
  12. Prints the text that is passed to it.
  13. optional arguments:
  14. -h, --help show this help message and exit
  15. usage:
  16. Simple example:
  17. ./param3.pl 'some test'
  18. Also supports piping the TEXT:
  19. echo 'some test' | ./param3.pl -\n");
  20. exit;
  21. }
  22. my $stdio;
  23. # An example subroutine that just prints whatever is passed to it.
  24. sub printit {
  25. print(shift . "\n");
  26. }
  27. # Process CLI parameters and update config values as necessary
  28. GetOptions ('<>' => \&printit,
  29. # $stdio is set to 1 if anything is piped
  30. '' => \$stdio,
  31. "h|help" => \&help_text)
  32. or die("Error in command line arguments. Please review and try again.\n");
  33. # Something is piped via stdin, so handle it
  34. if ($stdio) {
  35. my $input = <>;
  36. # Get rid of empty line at the end
  37. chomp $input;
  38. printit($input);
  39. }