Skip to content

Fix Exporter package name resolution and add AUTOLOAD support for imported forward declarations#112

Merged
fglock merged 1 commit intomasterfrom
fix/exporter-autoload-import
Dec 31, 2025
Merged

Fix Exporter package name resolution and add AUTOLOAD support for imported forward declarations#112
fglock merged 1 commit intomasterfrom
fix/exporter-autoload-import

Conversation

@fglock
Copy link
Copy Markdown
Owner

@fglock fglock commented Dec 31, 2025

Summary

This PR fixes the "Undefined subroutine" error when calling imported forward-declared functions that use AUTOLOAD.

Changes

1. Fixed Package Name Bug in Exporter.java

  • Issue: In exportToLevel(), the code was using packageScalar (RuntimeScalar object) instead of packageName (string) when looking up export arrays
  • Fix: Changed lines 158-160 to use packageName for correct namespace resolution
  • Impact: Export arrays are now correctly retrieved from the proper package namespace

2. Added AUTOLOAD Support for Imported Forward Declarations

  • File: RuntimeCode.java
  • Added: sourcePackage field to track the original package of imported forward declarations
  • Updated: apply() method to check for AUTOLOAD in the source package when an imported forward declaration is called
  • Impact: Enables proper AUTOLOAD resolution for modules that export forward-declared functions

3. Updated Import Mechanism

  • File: Exporter.java
  • Updated: importFunction() to set sourcePackage when importing undefined functions
  • Impact: Imported forward declarations now maintain their source package for correct AUTOLOAD resolution

Problem Solved

When a module like Image::ExifTool exports forward-declared functions (e.g., GetAllTags) that are later defined via AUTOLOAD, calling these functions after import would fail with "Undefined subroutine" errors. This was because:

  1. The package name wasn't being resolved correctly during export lookup
  2. Imported forward declarations didn't track their source package, so AUTOLOAD was checked in the wrong namespace

Testing

  • ✅ All 145 existing tests pass
  • ✅ Existing autoload.t test validates AUTOLOAD functionality
  • ✅ Code compiles cleanly

Example Use Case

This fix enables modules like ExifTool to work correctly:

use Image::ExifTool qw{GetAllTags};
my @tags = GetAllTags();  # Now correctly calls Image::ExifTool::AUTOLOAD

Note

While testing with ExifTool, we discovered a separate "Method too large" issue when compiling ExifTool's large data structures. Once that issue is resolved, the import mechanism will work correctly with these fixes in place.

…orted forward declarations

- Fixed bug in Exporter.exportToLevel() where packageScalar object was used instead of packageName string when looking up export arrays
- Added sourcePackage field to RuntimeCode to track original package of imported forward declarations
- Updated RuntimeCode.apply() to check sourcePackage AUTOLOAD when imported forward declarations are called
- This enables proper AUTOLOAD resolution for modules that export forward-declared functions (e.g., Image::ExifTool)

Fixes: Undefined subroutine errors when calling imported forward-declared functions
@fglock fglock merged commit 4a69e30 into master Dec 31, 2025
2 checks passed
@fglock fglock deleted the fix/exporter-autoload-import branch December 31, 2025 13:16
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

Successfully merging this pull request may close these issues.

1 participant