How can I write a CPAN module to support multiple versions of Perl?

Let's say I have a module on CPAN, and I would like to update it to use the features from the new Perls. Right now, as I understand it, if I do this, I put a line in the sand speaking from this version, you can use my module only if you have a version of X Perl.

Is there a clean / canonical way to support two different branches of the same module in CPAN?

ie, 2.x series will continue to be supported for versions prior to 5.8.x, while 3.x will be for version 5.16 +.

+6
source share
2 answers

The problem with having two branches with the same name is that cpan The::Module uselessly failing for some users (since it will always have the latest version). They could still install an older version of the module, but that would be much more cumbersome. Instead, change the module to

 package The::Module; do($] < 5.016 ? 'The/Module/Pre5016.pm' : 'The/Module/5016.pm') or die $@ || $!; 1; 

If only the individual parts of the module are different from each other, you can simply use

 sub _foo_compatible { ... } sub _foo_fast { ... } *foo = $] < 5.016 ? \&_foo_compatible : \&_foo_fast; 

This second method has the disadvantage that both subsites must compile in 5.8 (unless you add eval EXPR to the mix).

+4
source

The $] variable is deprecated in favor of the $^V variable , which contains the version of the current Perl interpreter as version (or undef if the version is higher than v5.6).

This allows you to compare the version with a string constant, for example v5.10 , which creates a packed string (containing the serial number of each version as a character code, so v5.10 eq "\x05\x0A" true).

Since v-strings are strings, you should compare them with the string comparators lt , le , eq , ge and gt , so you should write something like

 use v5.6; if ( $^V ge v5.10 ) { ... } 

But I wonder how your code will alternate between different versions of Perl? Most of the changes are syntactic, which offer a more convenient way to write certain constructs. Usually, you only need to write for the earliest version you want to support. It used to be v5.8, but v5.10 was a major revision, and many people assume that this is the minimum version required now that it is seven years old.

+5
source

Source: https://habr.com/ru/post/986069/


All Articles