Permalink
Browse files

Refactor some code into _get_lookup_table().

  • Loading branch information...
jkeenan committed Feb 22, 2010
1 parent 11512e3 commit d1c52e903a3cc1ca0b8f2cdd2f109d59b79ac4d7
Showing with 19 additions and 31 deletions.
  1. +19 −31 lib/CPAN/Mini/Visit/Simple.pm
@@ -153,33 +153,35 @@ sub refresh_list {
# the distribution name and the value is the version.
# We will make a similar hash from the derived list.
- my (%primary, %derived);
+ my %primary = _get_lookup_table( $self->get_list() );
+ my %derived = _get_lookup_table( @{ $args->{derived_list} } );
- foreach my $distro ( $self->get_list() ) {
- my $dir = dirname($distro);
- my $base = basename($distro);
- if ($base =~ m/^(.*)-([\d\.]+)(?:$ARCHIVE_REGEX)/) {
- my ($stem, $version) = ($1,$2);
- my $k = File::Spec->catfile($dir, $stem);
- $primary{$k} = {
- distro => $distro,
- version => normalize_version_number($version),
- };
+ foreach my $stem ( keys %derived ) {
+ if ( not exists $primary{$stem} ) {
+ delete $derived{$stem};
+ }
+ elsif ( $primary{$stem}{version} > $derived{$stem}{version} ) {
+ $derived{$stem}{version} = $primary{$stem}{version};
+ $derived{$stem}{distro} = $primary{$stem}{distro};
}
else {
- # Since we don't have any authoritative way to compare version
- # numbers that can't be normalized, we will (for now) pass over
- # distributions with non-standard version numbers.
+ # nothing to do
}
}
- foreach my $distro ( @{ $args->{derived_list} } ) {
+ return [ sort map { $derived{$_}{distro} } keys %derived ];
+}
+
+sub _get_lookup_table {
+ my @distributions = @_;
+ my %lookup_table = ();
+ foreach my $distro ( @distributions ) {
my $dir = dirname($distro);
my $base = basename($distro);
if ($base =~ m/^(.*)-([\d\.]+)(?:$ARCHIVE_REGEX)/) {
my ($stem, $version) = ($1,$2);
my $k = File::Spec->catfile($dir, $stem);
- $derived{$k} = {
+ $lookup_table{$k} = {
distro => $distro,
version => normalize_version_number($version),
};
@@ -190,21 +192,7 @@ sub refresh_list {
# distributions with non-standard version numbers.
}
}
-
- foreach my $stem ( keys %derived ) {
- if ( not exists $primary{$stem} ) {
- delete $derived{$stem};
- }
- elsif ( $primary{$stem}{version} > $derived{$stem}{version} ) {
- $derived{$stem}{version} = $primary{$stem}{version};
- $derived{$stem}{distro} = $primary{$stem}{distro};
- }
- else {
- # nothing to do
- }
- }
-
- return [ sort map { $derived{$_}{distro} } keys %derived ];
+ return %lookup_table;
}
1;

0 comments on commit d1c52e9

Please sign in to comment.