diff options
author | Jerry D. Hedden <jdhedden@cpan.org> | 2009-07-29 20:31:30 -0400 |
---|---|---|
committer | Jesse Vincent <jesse@bestpractical.com> | 2009-07-29 20:52:29 -0400 |
commit | 4e86fc4bd811aa6c664a88d3cef584224bf7f492 (patch) | |
tree | 0d5cc61b10fa33749cf9c4830e68384b410afb63 /Porting/manisort | |
parent | 11a595870adcd875f29635df8e4b9a2d22c36521 (diff) | |
download | perl-4e86fc4bd811aa6c664a88d3cef584224bf7f492.tar.gz |
Sort MANIFEST using Perl
Diffstat (limited to 'Porting/manisort')
-rw-r--r-- | Porting/manisort | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Porting/manisort b/Porting/manisort new file mode 100644 index 0000000000..1c02120573 --- /dev/null +++ b/Porting/manisort @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# Usage: manisort [-q] [-o outfile] [filename] +# +# Without 'filename', looks for MANIFEST in the current dir. +# With '-o outfile', writes the sorted MANIFEST to the specified file. +# Prints the result of the sort to stderr. '-q' silences this. +# The exit code for the script is the sort result status +# (i.e., 0 means already sorted properly, 1 means not properly sorted) + +use strict; +use warnings; +$| = 1; + +# Get command line options +use Getopt::Long; +my $outfile; +my $check_only = 0; +my $quiet = 0; +GetOptions ('output=s' => \$outfile, + 'check' => \$check_only, + 'quiet' => \$quiet); + +my $file = (@ARGV) ? shift : 'MANIFEST'; + +# Read in the MANIFEST file +open(my $IN, '<', $file) + or die("Can't read '$file': $!"); +my @manifest = <$IN>; +close($IN) or die($!); +chomp(@manifest); + +# Sort by dictionary order (ignore-case and +# consider whitespace and alphanumeric only) +my @sorted = sort { + (my $aa = $a) =~ s/[^\s\da-zA-Z]//g; + (my $bb = $b) =~ s/[^\s\da-zA-Z]//g; + uc($aa) cmp uc($bb) + } @manifest; + +# Check if the file is sorted or not +my $exit_code = 0; +for (my $ii = 0; $ii < $#manifest; $ii++) { + next if ($manifest[$ii] eq $sorted[$ii]); + $exit_code = 1; # Not sorted + last; +} + +# Output sorted file +if (defined($outfile)) { + open(my $OUT, '>', $outfile) + or die("Can't open output file '$outfile': $!"); + print($OUT join("\n", @sorted), "\n"); + close($OUT) or die($!); +} + +# Report on sort results +printf(STDERR "'$file' is%s sorted properly\n", + (($exit_code) ? ' NOT' : '')) if (! $quiet); + +# Exit with the sort results status +exit($exit_code); + +# EOF |