Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (11)
    As of 2015-07-02 19:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (44 votes), past polls