| tags: [ Perl Unicode ] categories: [ Development ]
Perl Unicode 脚本模版
参考 perldoc perl 里的以 perluni 开头的文档,至少用 Perl v5.36。
#!/usr/bin/env perl
# https://perldoc.perl.org/perluniintro#Perl's-Unicode-Support v5.28
# https://perldoc.perl.org/feature#The-'signatures'-feature v5.36
# https://perldoc.perl.org/perlunicook#℞-0:-Standard-preamble v5.36
use v5.36; # or later to get "unicode_strings" feature, plus strict and warnings
use utf8; # so literals and identifiers can be in UTF-8
use warnings qw(FATAL utf8); # fatalize encoding glitches
use open qw(:std :encoding(UTF-8)); # undeclared streams in UTF-8
use Encode qw(decode);
@ARGV = map { decode('UTF-8', $_, Encode::FB_CROAK) } @ARGV;
#use autodie qw(:all);
#use Unicode::Normalize; # for NFC(), NFD()
say "😊 3 加 4 等于 ", add(3, 4);
sub add($a, $b) {
return $a + $b;
}
如果是命令行执行 one-liner 脚本,最好使用如下命令:
perl -CSDA -Mutf8 -Mwarnings=FATAL,utf8 -lwE '...your-one-liner;'
其中 -CSDA
相当于 use open qw(:std :encoding(UTF-8); @ARGV=map{decode("UTF-8",$_,1)} @ARGV;
, -l
可以让 print
自动添加换行,-E
类似 -e
但额外引入 use vCURRENT
以开启最新特性,最重要的是 unicode_strings
特性。