in reply to Processing spreadsheet with some cells in ASCII, other cells in UTF-8
I think you are confused about encoding. It can be pretty confusing. See perlunitut, "Unicode and Strings" in Modern Perl, The Perl Unicode Cookbook ...
As you know, if you try to print a "wide" unicode character, Perl gives you a warning:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw/ say /;
say "Ferrari 308 \x{1F44D}";
__END__
$ perl 1140714.pl Wide character in say at 1140714.pl line 6. Ferrari 308 👍 $
#!/usr/bin/perl
use strict;
use warnings;
use feature qw/ say /;
binmode STDOUT, ':utf8';
say "Ferrari 308 \x{1F44D}";
__END__
$ perl 1140714.pl Ferrari 308 👍 $
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; say "Ferrari 308 👍"; __END__
$ perl 1140714.pl Ferrari 308 ð
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; use utf8; say "Ferrari 308 👍"; __END__
$ perl 1140714.pl Ferrari 308 👍 $
$ cat 1140714.txt Lotus Élan 👍 任意のスーパーカー $
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; use utf8; open my $in, '<', '1140714.txt' or die "open: $!\n"; print for (<$in>); __END__
$ perl 1140714.pl Lotus Ãlan ð ä»»æã®ã¹ã¼ãã¼
$ cat 1140714.pl #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; use utf8; open my $in, '< :utf8', '1140714.txt' or die "open: $!\n"; print for (<$in>); __END__
$ perl 1140714.pl Lotus Élan 👍 任意のスーパーカー
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; use utf8; open my $in, '< :utf8', '1140714.txt' or die "open: $!\n"; open my $out, '>', '1140714.out' or die "open: $!\n"; print $out $_ for (<$in>); close $out or die "close: $!\n"; __END__
$ perl 1140714.pl Wide character in print at 1140714.pl line 11, <$in> line 2. Wide character in print at 1140714.pl line 11, <$in> line 2. $
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; binmode STDOUT, ':utf8'; use utf8; open my $in, '< :utf8', '1140714.txt' or die "open: $!\n"; open my $out, '> :utf8', '1140714.out' or die "open: $!\n"; print $out $_ for (<$in>); close $out or die "close: $!\n"; __END__
$ perl 1140714.pl $ cat 1140714.out Lotus Élan 👍 任意のスーパーカー $
Hope this helps!
Update: Added examples and links
|
|---|