use Algorithm::Loops qw( NestedLoops ); my @special_chars = qw( : ; , = - ); my $corrupt_qfn = ...; my $iter = NestedLoops([ map { $_ eq " " ? \@special_chars : [ $_ ] } split( /( )/, $corrupt_qfn, -1 ) ]); while ( my @parts = $iter->() ) { my $possible_qfn = join( "", @parts ); say for $possible_qfn; }