File Coverage

blib/lib/String/Glob/Permute.pm
Criterion Covered Total %
statement 34 42 80.9
branch 12 16 75.0
condition 2 3 66.6
subroutine 3 3 100.0
pod 0 1 0.0
total 51 65 78.4


line stmt bran cond sub pod time code
1             # Copyright (c) 2008 Yahoo! Inc. All rights reserved. The copyrights to
2             # the contents of this file are licensed under the Perl Artistic License
3             # (ver. 15 Aug 1997).
4             ###########################################
5             package String::Glob::Permute;
6             ###########################################
7              
8 1     1   76033 use strict;
  1         2  
  1         43  
9 1     1   6 use warnings;
  1         2  
  1         14935  
10             require Exporter;
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(string_glob_permute);
13             our $VERSION = "0.01";
14              
15             ###########################################
16             sub string_glob_permute {
17             ###########################################
18 4     4 0 5383 my( $string_glob ) = @_;
19              
20 4         6 my @stringlist;
21 4         9 my @patterns = ($string_glob);
22              
23 4         13 while (@patterns) {
24              
25 24         35 my $h = shift(@patterns);
26              
27 24 100       95 if ($h =~ /^(.*?)\[([^\]]+)\]([^,{[]*)(,(.*))?$/) {
    100          
28 4         14 my($pre,$post) = ($1,$3);
29              
30 4         19 my @r = split(/,/,$2);
31              
32 4         10 for (@r) {
33              
34 8 50       35 if (/^(!?)(\d+)(-(\d+))?$/) {
35 8         50 my $lo = $2;
36 8 100       20 my $hi = $3 ? $4 : $lo;
37 8         10 my $fmt = "%d";
38              
39             # Expand [09-11] to ("09", "10", "11") instead of
40             # ("9", "10", "11").
41 8 100 66     42 if (length($lo) == length($hi) && length($lo) > 1) {
42 4         10 $fmt = "%0" . length($lo) . "d";
43             }
44              
45 8         34 for (my $n = $lo; $n <= $hi; $n++) {
46 14         42 my $hn = $pre . sprintf($fmt, $n) . $post;
47              
48 14 50       29 if ($1) {
49 0         0 @stringlist = grep { $_ ne $hn } @stringlist;
  0         0  
50 0         0 @patterns = grep { $_ ne $hn } @patterns;
  0         0  
51             } else {
52 14         51 push(@patterns,$hn);
53             }
54             }
55             } else {
56 0         0 warn("Unrecognized host pattern: $h");
57 0         0 return undef;
58             }
59             }
60              
61 4 50       22 if ($4) {
62 0         0 push(@patterns,$5);
63             }
64              
65             } elsif ($h =~ /^(.*?)\{([^\}]+)\}([^,[{]*)(,(.*))?$/) {
66              
67 3         9 my($pre,$post) = ($1,$3);
68 3         11 my @r = split(/,/,$2);
69              
70 3         7 for (@r) {
71 6         20 push(@patterns,$pre.$_.$post);
72             }
73              
74 3 50       17 if ($4) {
75 0         0 push(@patterns,$5);
76             }
77             } else {
78 17         44 my @h = split(/,/,$h);
79 17         50 push(@stringlist,@h);
80             }
81             }
82 4         23 return @stringlist;
83             }
84              
85             1;
86              
87             __END__