Created
March 29, 2013 15:27
-
-
Save skaji/5271533 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Data::Dumper; | |
=head1 SYNOPSIS | |
% perl evaluate.pl 'www[01-05].example[1-2].{com,jp}' | |
=cut | |
my @expression = @ARGV; | |
print Dumper evaluate(@expression); | |
sub evaluate { | |
choice( @{ range(@_) } ); | |
} | |
sub range { | |
my @expression = @_; | |
my @out; | |
while (my $str = shift @expression) { | |
my ($before, $start, $end, $after) | |
= $str =~ /([^\[]*) \[ (\d+) - (\d+) \] (.*)/xsm; | |
if (!defined $start) { | |
push @out, $str; | |
next; | |
} | |
my $format = join q//, ( | |
'%0', length $start == length $end ? length $start : '0', 'd' | |
); | |
push @expression, map { | |
join q//, $before, sprintf($format, $_), $after | |
} $start..$end; | |
} | |
return \@out; | |
} | |
sub choice { | |
my @expression = @_; | |
my @out; | |
while (my $str = shift @expression) { | |
my ($before, $choice, $after) | |
= $str =~ /([^\{]*) \{ ([^\}]+) \} (.*)/xsm; | |
if (!defined $choice) { | |
push @out, $str; | |
next; | |
} | |
push @expression, map { | |
join q//, $before, $_, $after | |
} split /\s*,\s*/, $choice; | |
} | |
return \@out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment