#!/usr/bin/perl require 5.8.0; use strict; use warnings; # Load in the modules we'll be using use URI; use Pod::Usage; use Getopt::Long; use File::Basename; my $switches = process_switches(); # User has goofed so be concise pod2usage( -verbose => 0) unless (@ARGV >= 1 and @ARGV <= 3); # User has asked for help so be helpful if ($switches->{help}) { pod2usage( -verbose => 1, -message => "More help: cherry.pl --man\n", -output => \*STDERR); } # User has asked for more help so be more helpful if ($switches->{man}) { pod2usage( -verbose => 2 -output => \*STDERR); } # This is our input URI my $uri = shift; # Split the URI into a host / path and filename my ($filepath, $filename) = get_file_path_name($uri); my $filesplit = get_split($filename, $switches); my ($min, $max) = set_limits(@ARGV, $filesplit->{digit}); # Use default precision if none was specified unless ($switches->{precision}) { $switches->{precision} = length($filesplit->{digit}); warn "Autodetected precision as $switches->{precision} digits.\n" if ($switches->{verbose}); } if ($switches->{verbose}) { if ($switches->{reverse}) { warn "Generating sequence from $max to $min.\n" if ($switches->{verbose}); } else { warn "Generating sequence from $min to $max.\n" if ($switches->{verbose}); } } # Print out the sequential list... for (my $i = $min; $i <= $max; $i++) { # Print the path and filename prefix print $filepath.$filesplit->{prefix}; unless ($switches->{reverse}) { # ...in ascending order print (sprintf "%0$switches->{precision}d", $i); } else { # ...or in descending order print (sprintf "%0$switches->{precision}d", $max - $i + 1); } # Print the filename suffix print $filesplit->{suffix}."\n"; } sub process_switches { my %switches; # Call Getopt::Long to handle any switches that might be present GetOptions ( 'help|h|?' => \$switches{help}, 'numeric-index|n:i' => \$switches{numindex}, 'precision|p:i' => \$switches{precision}, 'reverse|r' => \$switches{reverse}, 'verbose|v' => \$switches{verbose}, ); $switches{numindex} ||= -1; return \%switches; } sub get_file_path_name { # Get the URI that's been passed to the function my $input_uri = shift; # Create a new URI object based on it my $uri = URI->new($input_uri); # Extract the filename from the URI my ($filename) = fileparse($uri->path); # Slice the filename off a copy of the URI to get the full server / path my $filepath = $uri; $filepath =~ s/$filename//; return ($filepath, $filename); } sub set_limits { my $warn_flag = 1 if (@_ == 3); my $max = shift if (@_); my $min = shift if (@_); $min ||= 1; # Swap max and min if they're wrong if ($min > $max) { ($min, $max) = ($max, $min); warn "Waring: Given min is larger than max. Swapping values.\n" if ($switches->{verbose}); } $min += 0; $max += 0; return ($min, $max); } sub get_split { my $filename = shift; my $switches = shift; my %filesplit; my @digits = $filename =~ /(\d+)/g or die "Error: Could not extract a number from filename '$filename'.\n"; $filesplit{digit} = $digits[$switches->{numindex}]; ### This won't work for files like 01-file01.html ($filesplit{prefix}, $filesplit{suffix}) = split (/$filesplit{digit}/, $filename, 2); ### This won't work for files like 01-file01.html return \%filesplit; }