Search code examples
perlexportertypeglob

How do I rename an exported function in Perl?


I have some Perl modules which exports various functions. (We haven't used @EXPORT in new modules for some years, but have retained it for compatibility with old scripts.)

I have renamed a number of functions and methods to change to a consistent naming policy, and thought that then adding a list of lines like

*directory_error      = *directoryError;

at the end of the module would just alias the old name to the new.

This works, except when the old name is exported, and a calling script calls the function with an unqualified name: in this case, it reports that the subroutine is not found (in the calling module).

I guess that what is happening is that Exporter prepares the list in a BEGIN, when the alias has not been created; but I tried putting the typeglob assignment in a BEGIN block and that didn't help.

I've tried AUTOLOAD, but of course that does not make the name available in the calling context. Of course I could write a series of wrapper functions, but that is tedious. It's possible I could generate wrapper functions automatically, though I'm not sure how.

Any suggestions of a neat way of handling this?


Solution

  • The following works for me. This seems to be what you're describing; you must have made a mistake somewhere.

    Main script:

    use strict;
    use warnings;
    use Bar;
    
    baz();
    

    Module:

    package Bar;
    use strict;
    use warnings;
    
    require Exporter;
    our @ISA    = qw(Exporter);
    our @EXPORT = qw(baz);
    
    sub Baz { print "Baz() here\n" }
    
    *baz = *Baz;
    
    1;