#!/usr/bin/env perl
use strict;
use warnings;

use Perl::Metrics::Halstead;

# Possible metrics: effort, difficulty, volume, level, lang_level, intel_content, time_to_program, delivered_bugs
my $metric = shift || die "Usage: perl $0 metric file1.pm file2.pl [file3.pm...]\n";

die "Invalid Halstead metric: $metric\n" unless Perl::Metrics::Halstead->can($metric);

my @packages = @ARGV;

my @scores;

# Compute the Halstead metric for each given file.
for my $file (@packages) {
    next unless -e $file;
    my $m = Perl::Metrics::Halstead->new( file => $file );
    push @scores, [ $file, $m->$metric ];
}

# Output the sorted file list.
my $n = 0;
for my $i ( sort{ $a->[1] <=> $b->[1] } @scores ){
    printf "%d. %.4f %s\n", ++$n, $i->[1], $i->[0];
}
