Håkon Hægland
Håkon Hægland

Reputation: 40778

Transliteration with U option and capitalized option characters

I am looking at this source code example from the Net::Dict distribution. At line 61 there is:

$translation =~ y/[\200-\377]/[\200-\377]/UC;

I wonder if this was ever valid Perl code, and what it was supposed to do? I cannot get this to compile with perl 5.30. For example:

#! /usr/bin/env perl
use strict;
use warnings;
my $str = 'abc';
$str =~ y/[\200-\377]/[\200-\377]/UC;

gives output:

Bareword found where operator expected at ./p.pl line 5, near "y/[\200-\377]/[\200-\377]/UC"
syntax error at ./p.pl line 5, near "y/[\200-\377]/[\200-\377]/UC"
Execution of ./p.pl aborted due to compilation errors.

According to perldoc perlop there is no U option for the tr// operator.

Upvotes: 1

Views: 47

Answers (1)

Dada
Dada

Reputation: 6626

The /U and /C flag were valid in Perl 5.6.0, cf Perl 5.6.0's perldoc (I don't know when they were introduced though). They were removed in Perl 5.7.0, as you can see in perl570delta:

The tr///C and tr///U features have been removed and will not return; the interface was a mistake. Sorry about that. For similar functionality, see pack('U0', ...) and pack('C0', ...).

They were also removed from Perl 5.6.1; see perl561delta.

Note that the Net::Dict module was written in 1998, and the latest Perl version back then was 5.005. I guess that whomever updated the code since then missed this example.

The behavior of /U and /C in Perl 5.6.0 was:

U Translate to/from UTF-8.
C Translate to/from 8-bit char (octet).

[...]

The first /U or /C modifier applies to the left side of the translation. The second one applies to the right side. If present, these modifiers override the current utf8 state.

Upvotes: 3

Related Questions