Home > Software engineering >  Is it possible to print 'é' as 'é' in Perl?
Is it possible to print 'é' as 'é' in Perl?

Time:02-18

I have some string with accent like "é" and the goal is to put my string into an URL so I need to convert "é" to "é"

I have tested some module as HTML::Entitie, Encode or URI::Encode without any success

Actual Result: é

Expected Result: é

#!/usr/bin/perl
use strict;
use warnings;
use HTML::Entities;
use feature 'say';
use URI::Encode qw( uri_encode );

my $var = "é";
say $var;

$var = uri_encode( $var );

say $var;

CodePudding user response:

You didn't tell Perl what encoding the source code is in, so it assumes Latin-1 which is the default. Add

use utf8;

See utf8.

CodePudding user response:

You are missing use utf8.

The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope. The no utf8 pragma tells Perl to switch back to treating the source text as literal bytes in the current lexical scope. (On EBCDIC platforms, technically it is allowing UTF-EBCDIC, and not UTF-8, but this distinction is academic, so in this document the term UTF-8 is used to mean both).

Do not use this pragma for anything else than telling Perl that your script is written in UTF-8. The utility functions described below are directly usable without use utf8;.

  • Related