Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In general, it is vastly simpler to have two separate modules. That said, here's an example including test cases for a module which exports three functions (alphabet, colours and numbers) where two of the functions have different behaviours in API 1 and API 2.

The key trick here is using Sub::Exporter to install different coderefs into the caller's package depending on what API version they have requested. Some of the examples even show how a caller can request parts of API 1 and parts of API 2.

I've used an Exporter-type module because that's the most challenging to implement. If you're writing object-oriented code instead, then things are probably a bit easier. You can usually just create two subclasses of a base class.

use v5.14; use strict; use warnings; BEGIN { package Local::Module 2.00; no thanks; # this is just a helper for defining multiple # packages in the same file! # detect requested version, and store it my %api_preference; sub VERSION { $api_preference{ scalar caller } //= $_[1] if @_==2; shift->SUPER::VERSION( @_ ); } use Sub::Exporter; my $IMPORT = Sub::Exporter::build_exporter({ exports => [ alphabet => \&_build_alphabet, numbers => \&_build_numbers, colours => undef, ], collectors => [qw/ defaults auto /], }); sub import { push @_, auto => { api => ($api_preference{ scalar caller }//1 +.00) }; goto $IMPORT; } # This sub is the same for API 1 and API 2 sub colours { return [qw( red orange yellow green blue indigo violet )]; } # Version 1 API returns lower-case; version 2 API is uppercase sub _build_alphabet { my ($class, $name, $arg, $col) = @_; my $api = $arg->{api} // $col->{defaults}{api} // $col->{auto} +{api}; if ($api >= 2) { return sub { ['A'..'Z'] }; } else { return sub { ['a'..'z'] }; } } # Version 1 API returns 1..5; version 2 API returns 1..10. sub _build_numbers { my ($class, $name, $arg, $col) = @_; my $api = $arg->{api} // $col->{defaults}{api} // $col->{auto} +{api}; if ($api >= 2) { return sub { [1..10] }; } else { return sub { [1..5] }; } } }; TESTING: { use Test::More; # Expected results: my $alphabet1 = [ 'a'..'z' ]; my $alphabet2 = [ 'A'..'Z' ]; my $numbers1 = [ 1 .. 5 ]; my $numbers2 = [ 1 .. 10 ]; my $colours = [qw( red orange yellow green blue indigo violet )] +; # This consumer should get API 1 BEGIN { package Local::Consumer1 0.001; no thanks; use Local::Module 1.000 -all; }; is_deeply( Local::Consumer1::alphabet(), $alphabet1, 'Local::Consumer1 has alphabet() API 1', ); is_deeply( Local::Consumer1::numbers(), $numbers1, 'Local::Consumer1 has numbers() API 1', ); is_deeply( Local::Consumer1::colours(), $colours, 'Local::Consumer1 has colours() invariant', ); # This consumer should get API 2 BEGIN { package Local::Consumer2 0.001; no thanks; use Local::Module 2.000 -all; }; is_deeply( Local::Consumer2::alphabet(), $alphabet2, 'Local::Consumer2 has alphabet() API 2', ); is_deeply( Local::Consumer2::numbers(), $numbers2, 'Local::Consumer2 has numbers() API 1', ); is_deeply( Local::Consumer2::colours(), $colours, 'Local::Consumer2 has colours() invariant', ); # This consumer should get API 1 BEGIN { package Local::Consumer3 0.001; no thanks; use Local::Module -all; }; is_deeply( Local::Consumer3::alphabet(), $alphabet1, 'Local::Consumer3 has alphabet() API 1', ); is_deeply( Local::Consumer3::numbers(), $numbers1, 'Local::Consumer3 has numbers() API 1', ); is_deeply( Local::Consumer3::colours(), $colours, 'Local::Consumer3 has colours() invariant', ); # This consumer should get API 2, except for numbers() where they +want API 1 BEGIN { package Local::Consumer4 0.001; no thanks; use Local::Module 2.000 numbers => { api => 1 }, qw( colours alphabet ); }; is_deeply( Local::Consumer4::alphabet(), $alphabet2, 'Local::Consumer4 has alphabet() API 2', ); is_deeply( Local::Consumer4::numbers(), $numbers1, 'Local::Consumer4 has numbers() API 1', ); is_deeply( Local::Consumer4::colours(), $colours, 'Local::Consumer4 has colours() invariant', ); # This consumer wants both versions of the numbers() function BEGIN { package Local::Consumer5 0.001; no thanks; use Local::Module 2.000 numbers => { api => 1, -as => 'old_numbers' }, numbers => { api => 2 }, ; }; is_deeply( Local::Consumer5::numbers(), $numbers2, 'Local::Consumer5 has numbers() API 2', ); is_deeply( Local::Consumer5::old_numbers(), $numbers1, 'Local::Consumer5 has old_numbers() function which is number() + from API 1', ); done_testing; };
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: How do you do multi-version modules? by tobyink
in thread How do you do multi-version modules? by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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-29 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found