A basic recursive solution:
#!/usr/bin/perl
use strict;
use warnings;
my $phone_number = $ARGV[0] or die "No phone number";
my @nums = (
['0'],
['1'],
[ '2', 'a', 'b', 'c' ],
[ '3', 'd', 'e', 'f' ],
[ '4', 'g', 'h', 'i' ],
[ '5', 'j', 'k', 'l' ],
[ '6', 'm', 'n', 'o' ],
[ '7', 'p', 'q', 'r', 's' ],
[ '8', 't', 'u', 'v' ],
[ '9', 'w', 'x', 'y', 'z' ]
);
my %letters = map { shift @{$_} => $_ } @nums;
my @permutations;
sub recurse {
my $str = shift;
my $done = shift || '';
unless ($str) {
push @permutations, $done;
return;
}
my $next = substr( $str, 0, 1 );
$str = substr( $str, 1 );
recurse( $str, $done . $next );
if ( my @chars = @{ $letters{$next} } ) {
recurse( $str, $done . $_ ) foreach @chars;
}
}
recurse($phone_number);
print "$_\n" foreach @permutations;
and:
perl num2word 12
12
1a
1b
1c
perl num2word 213
213
21d
21e
21f
a13
a1d
a1e
a1f
b13
b1d
b1e
b1f
c13
c1d
c1e
c1f