Blog

carton + 開発版の cpanm で、特定のバージョンのモジュールをつかいこなす!

開発版の cpanm で、META file の version range がサポートされるようになりました。

CPAN の META spec v2.0 では、version range がサポートされていて、以下のような柔軟な指定が可能となっています。

Some fields (prereq, optional_features) indicate the particular version(s) of some other module that may be required as a prerequisite. This section details the Version Range type used to provide this information.

The simplest format for a Version Range is just the version number itself, e.g. 2.4. This means that at least version 2.4 must be present. To indicate that any version of a prerequisite is okay, even if the prerequisite doesn't define a version at all, use the version 0.

Alternatively, a version range may use the operators < (less than), <= (less than or equal), > (greater than), >= (greater than or equal), == (equal), and != (not equal). For example, the specification < 2.0 means that any version of the prerequisite less than 2.0 is suitable.

For more complicated situations, version specifications may be AND-ed together using commas. The specification >= 1.2, != 1.5, < 2.0 indicates a version that must be at least 1.2, less than 2.0, and not equal to 1.5.

http://search.cpan.org/~dagolden/CPAN-Meta-2.120921/lib/CPAN/Meta/Spec.pm#Version_Ranges

ですが、いままで CPAN コマンドや cpanm ではこの機能がサポートされていませんでした。

それが、開発版の cpanm コマンドでは実験的にサポートされるようになったのです。miyagawa++ といわざるをえませんね。

たとえば、みなさんが Class::DBI をつかうときには、version 3 系列はおかしなことになっているので、version 0.95 固定でつかっているかとおもいます。

そんなときも、Module::Build の Build.PL に以下のように記述するだけでよくなるのです。(もともと Module::Build は META Spec v2.0 に対応している)
なんか cpanfile じゃないとだめだそうなので cpanfile でやりましょう!

requires 'Class::DBI' => '== 0.95';

あとは carton install すれば、できあがり。

% carton install
Installing modules using Build.PL
You have Test::More (0.98)
You have Test::Requires (0.06)
Successfully installed UNIVERSAL-moniker-0.08
Successfully installed Class-Accessor-0.34
Successfully installed IO-stringy-2.110
Successfully installed Class-Trigger-0.14
Successfully installed Class-Data-Inheritable-0.08
Successfully installed DBI-1.623
Successfully installed DBIx-ContextualFetch-1.03
Successfully installed Ima-DBI-0.35
Successfully installed Class-DBI-0.95
You have Module::Build (0.39_01)
9 distributions installed
Complete! Modules were installed into local

見事に Class::DBI 0.95 がはいっているのがわかります。これで Class::DBI のバージョンが勝手にあがってしまうことを恐れる毎日から開放されますね! って、そんなやついねーか、ハハッ。

ここでは Class::DBI 0.95 を例にあげましたが、Moose になる前の Catalyst に依存しているとか、そういうケースにも安心ですね。また、メジャーバージョンアップのタイミングで非互換の変更をいれることを名言しているライブラリにたいしては

'FooWAF' => '>= 1.00, < 2.00',

みたいに指定すれば、安心ですね。

これは cpanm の次期安定板がたのしみでありますね!!!