12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #!/usr/bin/env perl
- # unicode.pl
- # Shows how to enable support for utf-8
- # License: CC0
- # Must read for troubleshooting or adapting:
- # https://newbedev.com/why-does-modern-perl-avoid-utf-8-by-default
- # To use decode_utf8
- require Encode;
- # For DATA file handles
- # Ref:
- # - https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node105.html
- # - https://www.perl.com/article/24/2013/5/11/Perl-tokens-you-should-know/
- binmode DATA, ":utf8";
- # Allow wide character outputs without warning
- binmode STDOUT, ":utf8";
- # To enable the use of unicode throughout perl functions
- use feature 'unicode_strings';
- # Since this script is UTF-8 encoded
- use utf8;
- # To handle CLI parameters
- use Getopt::Long;
- # Shows up when --help or -h is passed
- sub help_text {
- print("usage: unicode.pl [-h] [TEXT]
- Prints the text that is passed to it.
- optional arguments:
- -h, --help show this help message and exit
- usage:
- Simple example:
- ./unicode.pl 'some ইউনিকোড test'
- Also supports piping the TEXT:
- echo 'some ইউনিকোড test' | ./unicode.pl -\n");
- exit;
- }
- my $stdio;
- # An example subroutine that just prints whatever is passed to it.
- sub printit {
- print(Encode::decode_utf8(shift, 1) . "\n");
- }
- # Process CLI parameters and update config values as necessary
- GetOptions (# This is for parameter that isn't a parameter (e.g.
- # starting with -- or - or the value of them.)
- '<>' => \&printit,
- # This has the value of "1" when being piped something.
- '' => \$stdio,
- # To show help text when -h or --help is passed.
- "h|help" => \&help_text)
- or die("Error in command line arguments. Please review and try again.\n");
- # Process stdin
- if ($stdio) {
- # Get the piped string
- my $input = <>;
- # Get rid of extra empty line at the end
- chomp $input;
- # Send it to printit function
- printit($input);
- }
|