mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to rename a package without having to go and change all my code that uses it. I suppose this is like creating a package synonym.
My package name is MWLOG and I'd like to change it to Log::Writer.
So far what I've done seems to work but I'm checking here to see if I've done it the proper 'perl' way.
What I did was copy MWLOG to Log::Writer and then change MWLOG to be ...
package MWLOG; use strict; use warnings; use Log::Writer; our @ISA = qw(Log::Writer); our $VERSION = '1.06'; 1;
This seems to work. However, what I'd really like is for MWLOG's version to be the same as Log::Writer's version.
Changing the version to
our $VERSION = $Log::Writer::VERSION;
does not seem to work.
When I try and build my make file with I get the following error.
#perl Makefile.PL
WARNING: Setting VERSION via file 'MWLOG.pm' failed
 at /home/utils/perl/lib/perl5/5.8.0/ExtUtils/MakeMaker.pm line 523
Writing Makefile for MWLOG

Replies are listed 'Best First'.
Re: renaming a package
by dragonchild (Archbishop) on Apr 30, 2004 at 17:37 UTC
    What about Log::Writer->VERSION?

    Personally, I think you should go back and fix all your code, but that's just me.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      nope
      Log::Writer->VERSION
      doesn't work
      I get the same error when I run Makefile.PL
Re: renaming a package
by ysth (Canon) on Apr 30, 2004 at 21:16 UTC
    MakeMaker needs everything you need to calculate the version to be on a single line:
    use Log::Writer; our $VERSION = $Log::Writer::VERSION;
    A completely different approach to replace your entire file:
    *MWLOG:: = \%Log::Writer::;
    Do you have Makefile.PL set to extract the distribution's version from MWLOG.pm rather than Log::Writer.pm?
      my orignal post shows that I tried
      our $VERSION = $Log::Writer::VERSION;
      and although it works, MakeMaker doesn't like it. (see the error ^^^ that running perl Makefile.PL spews out when this line of code is in MWLOG.pm)

      do you know of anyway to get around that?
        Yes, put the use Log::Writer on the same line as I showed. MakeMaker extracts a single line from the file and eval's it; without the "use" on the same line, $Log::Writer::VERSION will be undefined (because MakeMaker won't have use'd it).
Re: renaming a package
by blue_cowdawg (Monsignor) on Apr 30, 2004 at 18:08 UTC

        I'm trying to rename a package without having to go and change all my code that uses it.

    I have to agree with dragonchild on this one and say that you probably are going to save yourself trouble in the long run if you just go back and fix code that calls your module by the old name. OR you could freeze the module in place at a point in time and just migrate code that needs the changes made to your newly renamed module.

    You could edit your old scripts by running something like:

    find . -depth -name '*.p[lm]' | xargs perl -spi -e 's/Log::Writer/MWLOG/'
    CAVEAT:Completely untested.

      It would be nice to change all those programs but the environment I work in would require formal test plans for each, and there are alot of them.
      Just thinking about the amount of paper work it would produce makes me dizzy.

            Just thinking about the amount of paper work it would produce makes me dizzy.

        Sounds like my shop. Then I'd just leave the older scripts on the old version until they required an upgrade and change them then.

Re: renaming a package
by demerphq (Chancellor) on May 01, 2004 at 08:51 UTC

    Changing the version to our $VERSION = $Log::Writer::VERSION; does not seem to work

    No it wont, at least certainly not from ExtUtils::MakeMaker perspective. Part of the reason is the rules for how $VERSION is evaluated (at least by CPAN) are fairly strict, and essentially involve parsing the file for a line that looks like a $VERSION assignment and then evaling it and using the result. So CPAN would definately get it wrong here.

    our $VERSION = do { require Log::Writer; $Log::Writer::VERSION;};

    Would at least keep CPAN type stuff happy.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi