Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7771181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:35:13+00:00 2026-06-01T16:35:13+00:00

Say, I have a sub that receives two arguments: An encoding specification, and a

  • 0

Say, I have a sub that receives two arguments: An encoding specification, and a file path. The sub then uses that information to open a file for reading as shown below, stripped down to its essentials:

run({
    encoding => 'UTF-16---LE',
    input_filename => 'test_file.txt',
});

sub run {
    my $args = shift;
    my ($enc, $fn) = @{ $args }{qw(encoding input_filename)};

    my $is_ok = open my $in,
        sprintf('<:encoding(%s)', $args->{encoding}),
        $args->{input_filename}
    ;
}

Now, this croaks with:

Cannot find encoding "UTF-16---LE" at E:\Home\...

What is the right way to ensure that $args->{encoding} holds a valid encoding specification before interpolating into the second argument to open?

Update

The information below is provided in the hope that it will be useful to someone at some point. I am also going to file a bug report.

The documents for Encode::Alias do not mention find_alias at all. A casual look at the Encode/Alias.pm on my Windows system reveals:

# Public, encouraged API is exported by default

our @EXPORT =
  qw (
  define_alias
  find_alias
);

However, note:

#!/usr/bin/env perl

use 5.014;
use Encode::Alias;
say find_alias('UTF-8')->name;

yields:

Use of uninitialized value $find in exists at C:/opt/Perl/lib/Encode/Alias.pm line 25.
Use of uninitialized value $find in hash element at C:/opt/Perl/lib/Encode/Alias.pm line 26.
Use of uninitialized value $find in pattern match (m//) at C:/opt/Perl/lib/Encode/Alias.pm line 31.
Use of uninitialized value $find in lc at C:/opt/Perl/lib/Encode/Alias.pm line 40.
Use of uninitialized value $find in pattern match (m//) at C:/opt/Perl/lib/Encode/Alias.pm line 31.
Use of uninitialized value $find in lc at C:/opt/Perl/lib/Encode/Alias.pm line 40.

Being 1) lazy, and 2) first to assume I am doing something wrong, I decided to seek others’ wisdom.

In any case, the bug is due to find_alias being exported as a function without checking for that in the code:

sub find_alias {
    require Encode;
    my $class = shift;
    my $find  = shift;
    unless ( exists $Alias{$find} ) {

If find_alias is not invoked as a method, the argument is now in $class and $find is undefined.

HTH.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:35:14+00:00Added an answer on June 1, 2026 at 4:35 pm

    You can use the find_encoding function in Encode. Although, if you want to use it as an :encoding layer, you should also check perlio_ok. It’s possible (but rare) for an encoding to exist but not support use with :encoding:

    use Carp qw(croak);
    use Encode qw(find_encoding);
    
    sub run {
        my $args = shift;
        my $enc = find_encoding($args->{encoding}) 
          or croak "$args->{encoding} is not a valid encoding";
        $enc->perlio_ok or croak "$args->{encoding} does not support PerlIO";
    
        my $is_ok = open my $in,
            sprintf('<:encoding(%s)', $enc->name),
            $args->{input_filename}
        ;
    }
    

    Note: find_encoding does handle aliases defined by Encode::Alias.

    If you don’t care about distinguishing between nonexistent encodings and those that don’t support :encoding, you can just use the perlio_ok function:

    Encode::perlio_ok($args->{encoding}) or croak "$args->{encoding} not supported";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have two classes, Foo and Foo Sub, each in a different
I have two websites: http://example.com http://example.com/sub I have a page, let's say: http://example.com/sub/page1 Is
Let's say I have this sub in VB.NET: ''' <summary> ''' Validates that <paramref
Say I have two apps, www.test.com and sub.test.com , now in sub.test.com , I
So say I have a URL being requested: sub.domain/file.bin and I wanted this file
Let's say I have UserStory1 and sub-tasks Task1, Task2 and Task3. Business decided that
Say I have a file, that contains some text. There are substrings like substr1,
Say I have a table: <table id=#myTable> <tr> Section 1 <td> <table> <tr> Sub
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
I have a mysql table that holds comments. The comments can have sub-comments, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.