Normalizing Perl variable names

Tom Christiansen filed RT 96814 noting that Perl identifiers (that is, variable names and such) should be normalized.

I covered this today in my Surviving Perl Unicode course at YAPC::EU in Rīga. Here's a program that output another Perl program that shows the problem:

use 5.010;
use utf8;
use charnames qw(:full);

my $var = time % 2 ? 
    "e\N{COMBINING ACUTE ACCENT}"
    :
    "\N{LATIN SMALL LETTER E WITH ACUTE}"; 


binmode STDOUT, ':encoding(UTF-8)';
print <<"PERL";
use utf8;
use 5.010;
use strict;
use warnings;

my \$e\N{COMBINING ACUTE ACCENT} = 'abc';
my \$\N{LATIN SMALL LETTER E WITH ACUTE} = '123';

\$$var = 'XYZ';

say "One char = ", \$\N{LATIN SMALL LETTER E WITH ACUTE};
say "Two char = ", \$e\N{COMBINING ACUTE ACCENT};
PERL

The output looks like:

use utf8;
use 5.010;
use strict;
use warnings;

my $é = 'abc';
my $é = '123';

$é = 'XYZ';

say "One char = ", $é;
say "Two char = ", $é;

Now, the trick is that by just looking at the source, you can't how many variables it has or what it is going to output. There are no strict violations or warnings.

It shouldn't matter to us how we made those variable names. They are the same idea no matter how I formed them, what my editor does to normalize, and so on.

Leave a comment

About brian d foy

user-pic I'm the author of Mastering Perl, and the co-author of Learning Perl (6th Edition), Intermediate Perl, Programming Perl (4th Edition) and Effective Perl Programming (2nd Edition).