[NBLUG/talk] I hate perl. :-)

William Tracy afishionado at gmail.com
Mon May 29 12:16:47 PDT 2006


I've been chasing down an internship lately. In the job description
they mentioned Perl, so I've been flipping through the Perl
documentation these last few days.

Boy, I miss Python. :-) Larry Wall must be a nut case. :-P (Arguments
to a subroutine are always passed as one big array? What the heck?)

Anyone who uses Perl for a program more than about fifty lines long
needs to be slapped.

At any rate, I finished my first Perl program. It takes a word and
generates all possible anagrams of it. (This is what Perl is supposed
to be good at, right?)

I *know* that there have to be a few Perl nuts here. ;-) Given the
nature of Perl nuts, I expect that someone will want to one-up my
program (who can write it as a one-liner?). So, here it is. Have at
it.

William








#!/usr/bin/env perl

if (@ARGV != 1) {
  die "Incorrect usage";
}

my $original = $ARGV[0];

print("Anagrams of ", $original, ":\n");

addAnagrams("", $original);

sub addAnagrams() {
  my $sofar = @_[0]; # The anagram so far
  my $left = @_[1];  # Letters not yet used in the anagram
  if(length($left) > 0) {
    for (my $counter = 0; $counter < length($left); ++$counter) {
      addAnagrams($sofar . substr($left, $counter, 1),
                  substr($left, 0, $counter) . substr($left, $counter+1));
    }
  } else {
    print $sofar, "\n";
  }
}



More information about the talk mailing list