unicode.pl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env perl
  2. # unicode.pl
  3. # Shows how to enable support for utf-8
  4. # License: CC0
  5. # Must read for troubleshooting or adapting:
  6. # https://newbedev.com/why-does-modern-perl-avoid-utf-8-by-default
  7. # To use decode_utf8
  8. require Encode;
  9. # For DATA file handles
  10. # Ref:
  11. # - https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node105.html
  12. # - https://www.perl.com/article/24/2013/5/11/Perl-tokens-you-should-know/
  13. binmode DATA, ":utf8";
  14. # Allow wide character outputs without warning
  15. binmode STDOUT, ":utf8";
  16. # To enable the use of unicode throughout perl functions
  17. use feature 'unicode_strings';
  18. # Since this script is UTF-8 encoded
  19. use utf8;
  20. # To handle CLI parameters
  21. use Getopt::Long;
  22. # Shows up when --help or -h is passed
  23. sub help_text {
  24. print("usage: unicode.pl [-h] [TEXT]
  25. Prints the text that is passed to it.
  26. optional arguments:
  27. -h, --help show this help message and exit
  28. usage:
  29. Simple example:
  30. ./unicode.pl 'some ইউনিকোড test'
  31. Also supports piping the TEXT:
  32. echo 'some ইউনিকোড test' | ./unicode.pl -\n");
  33. exit;
  34. }
  35. my $stdio;
  36. # An example subroutine that just prints whatever is passed to it.
  37. sub printit {
  38. print(Encode::decode_utf8(shift, 1) . "\n");
  39. }
  40. # Process CLI parameters and update config values as necessary
  41. GetOptions (# This is for parameter that isn't a parameter (e.g.
  42. # starting with -- or - or the value of them.)
  43. '<>' => \&printit,
  44. # This has the value of "1" when being piped something.
  45. '' => \$stdio,
  46. # To show help text when -h or --help is passed.
  47. "h|help" => \&help_text)
  48. or die("Error in command line arguments. Please review and try again.\n");
  49. # Process stdin
  50. if ($stdio) {
  51. # Get the piped string
  52. my $input = <>;
  53. # Get rid of extra empty line at the end
  54. chomp $input;
  55. # Send it to printit function
  56. printit($input);
  57. }