1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
|