| 1 | #!/usr/bin/perl -Ilib | ||
| 2 | |||
| 3 | =head2 marathon.pl | ||
| 4 | |||
| 5 | Example usage of the Acme::KeyboardMarathon module. Just give it filename(s) on | ||
| 6 | the command line as arguments and it returns the total distance. | ||
| 7 | |||
| 8 | $> ./marathon.pl foo.txt bar.txt baz.txt | ||
| 9 | |||
| 10 | =cut | ||
| 11 | |||
| 12 | use Acme::KeyboardMarathon; | ||
| 13 | use Math::BigInt lib => 'GMP'; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | my $akm = new Acme::KeyboardMarathon; | ||
| 18 | |||
| 19 | our @ARGV; | ||
| 20 | my $total = Math::BigInt->bzero(); | ||
| 21 | |||
| 22 | for my $file ( @ARGV ) { | ||
| 23 | print STDERR "Skipping [$file] as it is not a file\n" and next unless -f $file; | ||
| 24 | print STDERR "Skipping [$file] as it is a binary file\n" and next if $file =~ /\.(gif|gz|jpe?g|png|tar)$/o or -B $file; | ||
| 25 | print STDERR "Skipping [$file] as it is likely a git binary file\n" and next if $file =~ /\.git\//; | ||
| 26 | print STDERR "Reading [$file]\n"; | ||
| 27 | open INFILE, '<', $file; | ||
| 28 | while ( my $line = <INFILE> ) { | ||
| 29 | $total += $akm->distance($line); | ||
| 30 | } | ||
| 31 | close INFILE; | ||
| 32 | } | ||
| 33 | |||
| 34 | if ( $total > 100000 ) { | ||
| 35 | $total /= 100000; | ||
| 36 | print sprintf('%0.2f',$total) . " km\n"; | ||
| 37 | } elsif ( $total > 100 ) { | ||
| 38 | $total /= 100; | ||
| 39 | print sprintf('%0.2f',$total) . " m\n"; | ||
| 40 | } else { | ||
| 41 | print $total . " cm\n"; | ||
| 42 | } |