I'd be thinking in terms of:
- Loop through the number of possibilities. (2^length).
- Print the 'count' in binary, because that gives a bit mask.
- Use that bit mask to flip the bits on each iteration.
Something like this:
#!/usr/bin/env perl
use strict;
use warnings;
my $string = "abcde";
for my $mask ( 0 .. 2**length($string) - 1 ) {
my @bits = split( '', sprintf( "%0" . length($string) . "b\n", $mask ) );
for ( split( '', $string ) ) {
if ( shift @bits ) {tr/a-z/A-Z/}
print;
}
print "\n";
}
This works on pure alphabetical strings to the transformations, but it will always generated 2^length strings, and any character that isn't alphabetic will create duplicates.
'abcdef' will generate case variants to 'abcdeF, abcdEf, abcdEF...ABCDEF'. However '12345678' will generate 256 identical strings.
The easiest way of dealing with that case if it's problematic is to use a hash of strings rather than an array, and use keys
to extract the uniques.
However, as you note in your comments:
I need case-insensitive glob to find all files in folders with no matter to cases. As I couldn't get glob working case-insensitively, I thought that feeding it all possible cases of a string is the best way out. To my mind, there's no better language that Perl to cope with such things, but I don't know how to do it without monkeycode.
What you want is to be looking at File::Glob
- specifically the :nocase
parameter.