Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Change package name on the fly?

by pileofrogs (Priest)
on Sep 05, 2012 at 22:27 UTC ( [id://991961]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks.

I'm writing a test to make sure a new version of a perl module is compatible with an old version. The most obvious way to do that is to run the various functions from both versions and compare the result.

Obviously I can't just 'use' or 'require' the two modules, as they have the same package name and the same subroutine names and so on. The solution, it seems to me, would be to load the first version, then rename it's package somehow.

Something like this:

use Test::More; require 'old_lib/Foo/Bar.pm'; change_package_name('Foo::Bar','Old::Foo::Bar'); require 'lib/Foo/Bar.pm'; ok( Foo::Bar::baz('wibble') eq Old::Foo::Bar::baz('wibble'),'baz is co +mpatible'); ...etc...

Thanks!
--Pileofrogs

Replies are listed 'Best First'.
Re: Change package name on the fly?
by moritz (Cardinal) on Sep 06, 2012 at 07:01 UTC
Re: Change package name on the fly? (just test)
by tye (Sage) on Sep 06, 2012 at 00:48 UTC
    ok( Foo::Bar::­baz('wibbl­e') eq Old::Foo::­Bar::baz('­wibble'),'­baz +is compatible­');

    I'd just write:

    is( baz('wibble'), 'wibbled' );

    And then run the test suite against both versions. A test failing against the old version of the module might mean that you have to fixchange the test (if you really mean "backward compatible" as in "bug-for-bug compatible").

    - tye        

Re: Change package name on the fly?
by BrowserUk (Patriarch) on Sep 05, 2012 at 23:20 UTC

    You could try:

    1. Slurp the source code of (one of) the packages.
    2. Edit the package statement.
    3. eval the edited source into existence.

    Probably easier to just edit the name manually in the file until your testing is complete.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: Change package name on the fly?
by Plankton (Vicar) on Sep 06, 2012 at 05:41 UTC
    You could build and install the same version of Perl on your system twice. Just when you run Configure to build and install your second version use --prefix=/some/where/else. Then install the module you want to test in both Perl's. Something like
    $ /where/you/put/bin/perl -MCPAN shell cpan> install NWCLARK/Foo-Bar-0.02.tar.gz ... $ /some/where/else/bin/perl -MCPAN shell cpan> install NWCLARK/Foo-Bar-0.01.tar.gz
    Then in your test script just change the shebang.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://991961]
Approved by davido
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-19 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found