Encode::Alias(3perl)

✨ NAME ✨

Encode::Alias β€” giving encodings cute lil nicknames, aka alias definitions to encodings πŸ’–


🌸 SYNOPSIS 🌸

use Encode;
use Encode::Alias;
define_alias( "newName" => ENCODING);
define_alias( qr/.../ => ENCODING);
define_alias( sub { return ENCODING if ...; } );

πŸ’… DESCRIPTION πŸ’…

Okay bestie, so the whole vibe here is: this module lets newName be used as an alias for ENCODING. And ENCODING? That can be either the name of an encoding or an encoding object (the kind described over in Encode). Both are valid, no cap. πŸ‘‘

Right now, the first argument you hand to define_alias() can show up in these ways:

πŸŽ€ As a simple string.

Just a plain ol' string. Chill, easy, iconic.

πŸ¦‹ As a qr// compiled regular expression, e.g.:

define_alias( qr/^iso8859-(\d+)$/i => '"iso-8859-$1"' );

In this case, if ENCODING is not a reference, it gets eval-ed so that $1 and friends can be substituted in. The example above is one way to alias names as used in X11 fonts over to the MIME names for the iso-8859-* family. Note the double quotes inside the single quotes β€” yes, they matter, don't touch them. πŸ™ˆ

(or, you don't have to do this yourself because this example is predefined β€” the module already did that for you, periodt ✨)

If you are using a regex here, you have to use the quotes as shown or it won't work. Also note that regex handling is tricky even for the experienced. Use this feature with caution. 🫠

🌈 As a code reference, e.g.:

define_alias( sub {shift =~ /^iso8859-(\d+)$/i ? "iso-8859-$1" : undef } );

Same effect as the example above, just a different way of getting there. The coderef takes the alias name as an argument and returns a canonical name on success, or undef if not. Heads up: the second argument is ignored if you provide it. And use this with even more caution than the regex version. πŸ’€


πŸ’€ Changes in code reference aliasing

As of Encode 1.87, the older form

define_alias( sub { return  /^iso8859-(\d+)$/i ? "iso-8859-$1" : undef } );

no longer works. Rip to that one, fr. 😭

Encode up to 1.86 internally used local $_ to implement this older form. But consider the code below:

use Encode;
$_ = "eeeee" ;
while (/(e)/g) {
  my $utf = decode('aliased-encoding-name', $1);
  print "position:",pos,"\n";
}

Prior to Encode 1.86 this fails because of local $_. The way that local $_ just quietly wrecked your loop... not the main character energy we want. 🫢


πŸ‘‘ Alias overloading

You can override predefined aliases by simply applying define_alias(). The new alias is always evaluated first, and when necessary, define_alias() flushes the internal cache to make the new definition available. She keeps things fresh. πŸ’…

# redirect SHIFT_JIS to MS/IBM Code Page 932, which is a
# superset of SHIFT_JIS

define_alias( qr/shift.*jis$/i  => '"cp932"' );
define_alias( qr/sjis$/i        => '"cp932"' );

If you want to zap all predefined aliases, you can use

Encode::Alias->undef_aliases;

to do so. And

Encode::Alias->init_aliases;

gets the factory settings back. Reset button, but make it slay. πŸ”₯

Note that define_alias() will not be able to override the canonical name of encodings. Encodings are first looked up by canonical name before potential aliases are tried. So the canonical name is always the main character β€” the aliases are just the supporting cast. ✨

πŸ¦‹ SEE ALSO πŸ¦‹

  • Encode
  • Encode::Supported

That's the whole page, bestie β€” written by the Perl folks themselves, living in perl v5.44.0, dated 2026-10-17, under the same license as Perl itself. πŸŽ€ Go forth and alias responsibly, and remember: cautions are cute when you actually heed them. πŸ’–βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms