Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"define_alias" no longer works with just "use Encode::Alias" #116

Closed
arc opened this issue Jul 16, 2017 · 5 comments
Closed

"define_alias" no longer works with just "use Encode::Alias" #116

arc opened this issue Jul 16, 2017 · 5 comments

Comments

@arc
Copy link

arc commented Jul 16, 2017

In Encode 2.91, this code

use Encode::Alias;
use open ":std", ":locale";

yields this error:

Undefined subroutine &Encode::define_alias called at lib/Encode.pm line 102.
Compilation failed in require at lib/Encode/Alias.pm line 7.
BEGIN failed--compilation aborted at lib/Encode/Alias.pm line 7.
Compilation failed in require at -e line 1.
BEGIN failed--compilation aborted at -e line 1.

This was discovered when trying to import 2.91 into blead, because the Perl test suite does precisely that.

Reverting the change that 55364c9 made to lib/Encode/Alias.pm makes this problem go away. That may not be the ideal fix, though.

Since there's a circular dependency here — Encode.pm does use Encode::Alias, and Encode/Alias.pm does use Encode () perhaps it would be better to break the shared pieces out into a separate (internal) module that both Encode and Encode::Alias use?

@pali
Copy link
Contributor

pali commented Jul 16, 2017

It is not simple... because functions from Encode.pm calls functions from Encode/Alias.pm and also functions from Encode/Alias.pm calls functions from Encode.pm...

Looks like those functions from Enocde.pm and Encode/Alias.pm needs to be moved into one module... I'm thinking about merging Encode/Alias.pm into Encode.pm. But because Encode/Alias.pm has global variables there can be other problems.

@arc what do you think?

@pali
Copy link
Contributor

pali commented Jul 17, 2017

@arc Here is less invasive patch which could to work. Can you test if it works for you too?

diff --git a/Encode.pm b/Encode.pm
index a38bf0f..62a143c 100644
--- a/Encode.pm
+++ b/Encode.pm
@@ -49,7 +49,7 @@ our %EXPORT_TAGS = (
 
 our $ON_EBCDIC = ( ord("A") == 193 );
 
-use Encode::Alias;
+use Encode::Alias ();
 use Encode::MIME::Name;
 
 use Storable;
@@ -138,6 +138,15 @@ sub getEncoding {
     return;
 }
 
+# HACK: These two functions must be defined in Encode and because of
+# cyclic dependency between Encode and Encode::Alias, Exporter does not work
+sub find_alias {
+    goto &Encode::Alias::find_alias;
+}
+sub define_alias {
+    goto &Encode::Alias::define_alias;
+}
+
 sub find_encoding($;$) {
     my ( $name, $skip_external ) = @_;
     return __PACKAGE__->getEncoding( $name, $skip_external );
diff --git a/lib/Encode/Alias.pm b/lib/Encode/Alias.pm
index 7020e1e..eb73b87 100644
--- a/lib/Encode/Alias.pm
+++ b/lib/Encode/Alias.pm
@@ -4,8 +4,6 @@ use warnings;
 our $VERSION = do { my @r = ( q$Revision: 2.22 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
 use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG};
 
-use Encode ();
-
 use Exporter 'import';
 
 # Public, encouraged API is exported by default
@@ -109,6 +107,9 @@ sub define_alias {
     }
 }
 
+# HACK: Encode must be used after define_alias is declarated as Encode calls define_alias
+use Encode ();
+
 # Allow latin-1 style names as well
 # 0  1  2  3  4  5   6   7   8   9  10
 our @Latin2iso = ( 0, 1, 2, 3, 4, 9, 10, 13, 14, 15, 16 );

@arc
Copy link
Author

arc commented Jul 17, 2017

Yes, importing Encode 2.91 into blead and then applying that patch makes blead's tests pass. Thanks!

I'll hold off on actually importing a newer Encode until there's a release including this change.

pali added a commit to pali/p5-encode that referenced this issue Jul 17, 2017
Calling

  use Encode::Alias;

cased error:

  Undefined subroutine &Encode::define_alias called at lib/Encode.pm line 102.
  Compilation failed in require at lib/Encode/Alias.pm line 7.
  BEGIN failed--compilation aborted at lib/Encode/Alias.pm line 7.
  Compilation failed in require at -e line 1.
  BEGIN failed--compilation aborted at -e line 1.

Fixes dankogai#116
@pali
Copy link
Contributor

pali commented Jul 17, 2017

Thanks for testing, I created pull request: #119

@pali
Copy link
Contributor

pali commented Jul 18, 2017

@arc New version with this fix was released.

rurban pushed a commit to rurban/p5-encode that referenced this issue May 10, 2019
Calling

  use Encode::Alias;

cased error:

  Undefined subroutine &Encode::define_alias called at lib/Encode.pm line 102.
  Compilation failed in require at lib/Encode/Alias.pm line 7.
  BEGIN failed--compilation aborted at lib/Encode/Alias.pm line 7.
  Compilation failed in require at -e line 1.
  BEGIN failed--compilation aborted at -e line 1.

Fixes dankogai#116
khwilliamson pushed a commit to khwilliamson/p5-encode that referenced this issue May 2, 2020
Calling

  use Encode::Alias;

cased error:

  Undefined subroutine &Encode::define_alias called at lib/Encode.pm line 102.
  Compilation failed in require at lib/Encode/Alias.pm line 7.
  BEGIN failed--compilation aborted at lib/Encode/Alias.pm line 7.
  Compilation failed in require at -e line 1.
  BEGIN failed--compilation aborted at -e line 1.

Fixes dankogai#116
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants