param2.pl 960 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env perl
  2. # Perl parameter example with Getopt::Long, with optional param.
  3. # License: CC0
  4. # Usage:
  5. # ./param2.pl 'some test text'
  6. # ./param2.pl -p 'append test' 'some test'
  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: param1.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. -p APPEND_TEXT, --append APPEND_TEXT
  16. append some text with print (optional)\n");
  17. exit;
  18. }
  19. my $append;
  20. # An example subroutine that just prints whatever is passed to it.
  21. sub printit {
  22. print(shift . "\n");
  23. print("$append\n") if ($append);
  24. }
  25. # Process CLI parameters and update config values as necessary
  26. GetOptions ('<>' => \&printit,
  27. "p|append=s" => \$append,
  28. "h|help" => \&help_text)
  29. or die("Error in command line arguments. Please review and try again.\n");