/usr/share/perl5/String/Glob/Permute.pm is in libstring-glob-permute-perl 0.01-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | # Copyright (c) 2008 Yahoo! Inc. All rights reserved. The copyrights to
# the contents of this file are licensed under the Perl Artistic License
# (ver. 15 Aug 1997).
###########################################
package String::Glob::Permute;
###########################################
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(string_glob_permute);
our $VERSION = "0.01";
###########################################
sub string_glob_permute {
###########################################
my( $string_glob ) = @_;
my @stringlist;
my @patterns = ($string_glob);
while (@patterns) {
my $h = shift(@patterns);
if ($h =~ /^(.*?)\[([^\]]+)\]([^,{[]*)(,(.*))?$/) {
my($pre,$post) = ($1,$3);
my @r = split(/,/,$2);
for (@r) {
if (/^(!?)(\d+)(-(\d+))?$/) {
my $lo = $2;
my $hi = $3 ? $4 : $lo;
my $fmt = "%d";
# Expand [09-11] to ("09", "10", "11") instead of
# ("9", "10", "11").
if (length($lo) == length($hi) && length($lo) > 1) {
$fmt = "%0" . length($lo) . "d";
}
for (my $n = $lo; $n <= $hi; $n++) {
my $hn = $pre . sprintf($fmt, $n) . $post;
if ($1) {
@stringlist = grep { $_ ne $hn } @stringlist;
@patterns = grep { $_ ne $hn } @patterns;
} else {
push(@patterns,$hn);
}
}
} else {
warn("Unrecognized host pattern: $h");
return undef;
}
}
if ($4) {
push(@patterns,$5);
}
} elsif ($h =~ /^(.*?)\{([^\}]+)\}([^,[{]*)(,(.*))?$/) {
my($pre,$post) = ($1,$3);
my @r = split(/,/,$2);
for (@r) {
push(@patterns,$pre.$_.$post);
}
if ($4) {
push(@patterns,$5);
}
} else {
my @h = split(/,/,$h);
push(@stringlist,@h);
}
}
return @stringlist;
}
1;
__END__
=head1 NAME
String::Glob::Permute - Expand {foo,bar,baz}[2-4] style string globs
=head1 SYNOPSIS
use String::Glob::Permute qw( string_glob_permute );
my $pattern = "host{foo,bar,baz}[2-4]";
for my $host (string_glob_permute( $pattern )) {
print "$host\n";
}
# hostfoo2
# hostbar2
# hostbaz2
# hostfoo3
# hostbar3
# hostbaz3
# hostfoo4
# hostbar4
# hostbaz4
=head1 DESCRIPTION
The C<string_glob_permute()> function provided by this module expands
glob-like notations in text strings and returns all possible permutations.
For example, to run a script on hosts host1, host2, and host3, you might
write
@hosts = string_glob_permute( "host[1-3]" );
and get a list of hosts back: ("host1", "host2", "host3").
Ranges with gaps are also supported, just separate the blocks by
commas:
@hosts = string_glob_permute( "host[1-3,5,9]" );
will return ("host1", "host2", "host3", "host5", "host9").
And, finally, using curly brackets and comma-separated lists of strings,
as in
@hosts = string_glob_permute( "host{dev,stag,prod}" );
you'll get permutations with each of the alternatives back:
("hostdev", "hoststag", "hostprod") back.
All of the above can be combined, so
my @hosts = string_glob_permute( "host{dev,stag}[3-4]" );
will result in the permutation
("hostdev3", "hoststag3", "hostdev4", "hoststag4").
The patterns allow numerical ranges only [1-3], no string ranges like
[a-z]. Pattern must not contain blanks.
The function returns a list of string permutations on success and
C<undef> in case of an error. A warning is also issued if the pattern
cannot be recognized.
=head2 Zero padding
An expression like
@hosts = string_glob_permute( "host[8-9,10]" );
# ("host8", "host9", "host10")
will expand to ("host8", "host9", "host10"), featuring no zero-padding to
create equal-length entries. If you want ("host08", "host09", "host10"),
instead, pad all integers in the range expression accordingly:
@hosts = string_glob_permute( "host[08-09,10]" );
# ("host08", "host09", "host10")
=head2 Note on Perl's internal Glob Permutations
Note that there's a little-known feature within Perl itself that does
something similar, for example
print "$_\n" for < foo{bar,baz} >;
will print
foobar
foobaz
if there is no file in the current directory that matches that pattern.
String::Glob::Permute, on the other hand, expands irrespective of matching
files, by simply always returning all possible permutations. It's also
worth noting that Perl's internal Glob Permutation does not support
String::Glob::Permute's [m,n] or [m-n] syntax.
=head1 COPYRIGHT & LICENSE
Copyright (c) 2008 Yahoo! Inc. All rights reserved. The copyrights to
the contents of this file are licensed under the Perl Artistic License
(ver. 15 Aug 1997).
=head1 AUTHOR
Algorithm, Code: Rick Reed, Ryan Hamilton, Greg Olszewski.
Module: 2008, Mike Schilli <cpan@perlmeister.com>
|