Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uppercase accented characters in Perl

Tags:

uppercase

perl

Is there a way to uppercase accented characters in Perl?

my $string = "éléphant";

print uc($string);

So that it actually prints ÉLÉPHANT?

My Perl script is encoded in ISO-8859-1 and $string is printed in an XML file with the same encoding.

like image 676
Fred Avatar asked Oct 25 '25 06:10

Fred


1 Answers

perl only understands US-ASCII[1] and UTF-8,[2] and the latter requires

use utf8;

If you want to keep the file as iso-8859-1, you'll need to decode the text explicitly.

use open ':std', ':encoding(locale)';
use Encode qw( decode );

# Source is encoded using iso-8859-1, so we need to decode ourselves.
my $string = decode("iso-8859-1", "éléphant");
print uc($string);

But it's probably better to convert the script to UTF-8.

use utf8;  # Source is encoded using UTF-8
use open ':std', ':encoding(locale)';

my $string = "éléphant";
print uc($string);

If you're printing to a file, make sure you use :encoding(iso-8859-1) when you open the file (no matter which alternative you use).


  1. Or EBCDIC on EBCDIC-based machines.
  2. Some also accept other UTF encodings under some conditions.
like image 113
ikegami Avatar answered Oct 26 '25 19:10

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!