#!/usr/bin/perl -w # chipher PATTERN [< WORDLIST] - search dictionary for words # # lowercase letters match literally # uppercase letters all refer to the same unknown letter # * matches any sequence of letters use strict; if (-t) { open(STDIN, "<", "/usr/share/dict/words") or die "no more words: $!\n"; } $_ = shift; my @avoid = do { my @lits = /[a-z]/g; @lits ? "[" . join("", @lits) . "]" : () }; my %template; my $regex = "^"; for (split //) { if (/[a-z]/) { $regex .= "$_"; } elsif (/[A-Z]/) { if (exists $template{$_}) { $regex .= $template{$_}; } else { my $id = 1 + keys %template; if (@avoid) { $regex .= "(?!" . join("|", @avoid) . ")"; } $regex .= "(.)"; push @avoid, $template{$_} = "\\$id"; } } elsif (/\*/) { $regex .= ".*"; } else { warn "ignoring $_"; } } $regex .= "\$"; warn "$_ => $regex\n"; my $matches = 0; while (<>) { if (/$regex/i) { print; $matches++; } } exit !$matches;