DhMakePerl::Utils - helper routined for dh-make-perl and alike


DhMakePerl documentation Contained in the DhMakePerl distribution.

Index


Code Index:

NAME

Top

DhMakePerl::Utils - helper routined for dh-make-perl and alike

SYNOPSIS

Top

    use DhMakePerl::Utils qw(is_core_module);

    my $v = is_core_module('Test::More', '1.002');
    my $v = nice_perl_ver('5.010001');

FUNCTIONS

Top

None of he following functions is exported by default.

find_cpan_module

Returns CPAN::Module object that corresponds to the supplied argument. Returns undef if no module is found by CPAN.

If the CPAN module needs to be configured in some way, that should be done before calling this function.

find_cpan_distribution

Returns CPAN::Distribution object that corresponds to the supplied argument. Returns undef if no distribution is found by CPAN.

If the CPAN module needs to be configured in some way, that should be done before calling this function.

is_core_module module, version

Returns the version of the perl package containing the given module (at least version version).

Returns undef if module is not a core module.

nice_perl_ver version_string

Reformats perl version to match Debian's perl package versions.

For example 5.010 (and 5.01) is converted to 5.10.

core_module_perls module[, min-version]

Returns a list of Perl versions that have module. If min-version is given, the list contains only Perl versions containing module at least version min-version.

find_core_perl_dependency( $module[, $version] )

return a dependency on perl containing the required module version. If the module is not available in any perl released by Debian, return undef.

split_version_relation string

Splits the string, typicaly found in dependency fields' values in CPAN META into relation and version. If no relation is found in the string, >= is assumed.

Returns a list of relation and version. The relation is suitable for using in debian package dependency version requirements.

For example

split_version_relation('0.45') returns ( '>=', '0.45' )
split_version_relation('< 0.56') returns ( '<<', '0.56' )

COPYRIGHT & LICENSE

Top


DhMakePerl documentation Contained in the DhMakePerl distribution.
package DhMakePerl::Utils;

our @EXPORT_OK = qw(
    find_core_perl_dependency
    find_cpan_module find_cpan_distribution
    is_core_module
    nice_perl_ver
    split_version_relation
);

use base Exporter;

use 5.10.0;

use Module::CoreList ();
use Debian::Dependency;

sub find_cpan_module {
    my( $name ) = @_;

    my $mod;

    # expand() returns a list of matching items when called in list
    # context, so after retrieving it, we try to match exactly what
    # the user asked for. Specially important when there are
    # different modules which only differ in case.
    #
    # This Closes: #451838
    my @mod = CPAN::Shell->expand( 'Module', '/^' . $name . '$/' );

    foreach (@mod) {
        my $file = $_->cpan_file();
        $file =~ s#.*/##;          # remove directory
        $file =~ s/(.*)-.*/$1/;    # remove version and extension
        $file =~ s/-/::/g;         # convert dashes to colons
        if ( $file eq $name ) {
            $mod = $_;
            last;
        }
    }
    $mod = shift @mod unless ($mod);

    return $mod;
}

sub find_cpan_distribution {
    my( $name ) = @_;

    $name =~ s/::/-/g;

    return CPAN::Shell->expand( 'Distribution',
        "/\\/$name-[^\\/]+\\.(tar|zip)/" );
}

sub is_core_module {
    my ( $module, $ver ) = @_;

    my $v = Module::CoreList->first_release($module, $ver);   # 5.009002

    return unless defined $v;

    $v = version->new($v);                              # v5.9.2
    ( $v = $v->normal ) =~ s/^v//;                      # "5.9.2"

    return $v;
}

sub nice_perl_ver {
    my( $v ) = @_;

    if( $v =~ /\.(\d+)$/ and $v !~ /\..+\./ ) { # do nothing for 5.9.1
        my $minor = $1;
        if( length($minor) % 3 ) {
            # right-pad with zeroes so that the number of digits after the dot
            # is a multiple of 3
            $minor .= '0' x ( 3 - length($minor) % 3 );
        }

        my $ver = 0 + substr( $minor, 0, 3 );
        if( length($minor) > 3 ) {
            $ver .= '.' . ( 0 + substr( $minor, 3 ) );
        }
        $v =~ s/\.\d+$/.$ver/;

        $v .= '.0' if $v =~ /^\d+\.\d+$/;   # force three-component version
    }

    return $v;
}

sub core_module_perls {
    my( $module, $version ) = @_;

    my @ret;

    $version = version->new($version) if $version;

    for my $v(
        sort keys %Module::CoreList::version ){

        # Module::CoreList::version includes families (i.e. "5") as well as
        # full versions, skip the families.
        next unless ($v =~ /^\d+\.\d+(?:\.|$)/);

        next unless exists $Module::CoreList::version{$v}{$module};

        my $found = $Module::CoreList::version{$v}{$module};

        push @ret, $v
            if not $version
                or $found and version->new($found) >= $version;
    }

    return @ret;
}

our %debian_perl = (
    '5.8'   => {
        min => Dpkg::Version->new('5.8.8'),
        max => Dpkg::Version->new('5.8.8'),
    },
    '5.10'  => {
        min => Dpkg::Version->new('5.10.0'),
        max => Dpkg::Version->new('5.10.1'),
    },
);

sub find_core_perl_dependency {
    my ( $module, $version ) = @_;

    if ( $module eq 'perl' ) {
        return Debian::Dependency->new('perl') unless $version;

        return Debian::Dependency->new( 'perl', nice_perl_ver($version) );
    }

    my $perl_dep;

    my @perl_releases = core_module_perls( $module, $version );

    for my $v (@perl_releases) {
        $v = nice_perl_ver($v);

        $v =~ /^(\d+\.\d+)(?:\.|$)/;
        my $major = $1 or die "[$v] is a strange version";

        # we want to avoid depending on things like 5.8.9 which aren't in
        # Debian and can contain stuff newer than in 5.10.0
        if (    $debian_perl{$major}
            and $debian_perl{$major}{max} >= $v )
        {
            return Debian::Dependency->new( 'perl', $v );
        }
    }

    # not a core module
    return undef;
}

sub split_version_relation {
    my $in = shift;

    $in =~ s/^\s*([<>=!])\s*//;

    my $rel = $1 // '>=';

    $rel = '>>' if  $rel eq '>';

    $rel = '<<' if $rel eq '<';

    return ( $rel, $in );
}

1; # End of DhMakePerl