title: Perl Recursion and Functions
body: <p>Having heard about Perl for year I decided to give it a few hours of my time to see how much I could pick up. I got through the basics fine and then got to loops. As a test I wanted to see if I could build a script to recurse through all alphanumerical values of up to 4 characters. I had written a PHP code that did the same thing some time ago so I took the same concept and used it. However when I run the script it puts "a" as the first 3 values and then only loops through the last digit. Anyone see what I am doing wrong?</p>
  
  <pre><code>#!/usr/local/bin/perl 
  
  $chars = "abcdefghijklmnopqrstuvwxyz";
  $chars .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $chars .= "0123456789";
  
  @charset = split(//, $chars);
  
  $charset_length = scalar(@charset);
  
  sub recurse
  {
   ($width, $position, $base_string) = @_;
  
  for ($i = 0; $i &lt; $charset_length; ++$i) {
      $base = $base_string . $charset[$i];
      if ($position &lt; $width - 1) {
          $pos = $position + 1;
          recurse($width, $pos, $base);
      }
      print $base;
      print "\n";
  }
  }
  
  recurse(4, 0, '');
  </code></pre>
  
  <p>This is what I get when I run it:</p>
  
  <pre><code>aaaa
  aaab
  aaac
  aaad
  aaae
  aaaf
  aaag
  aaah
  aaai
  aaaj
  aaak
  aaal
  aaam
  aaan
  aaao
  aaap
  aaaq
  aaar
  aaas
  aaat
  aaau
  aaav
  aaaw
  aaax
  aaay
  aaaz
  aaaA
  aaaB
  aaaC
  aaaD
  aaaE
  aaaF
  aaaG
  aaaH
  aaaI
  aaaJ
  aaaK
  aaaL
  aaaM
  aaaN
  aaaO
  aaaP
  aaaQ
  aaaR
  aaaS
  aaaT
  aaaU
  aaaV
  aaaW
  aaaX
  aaaY
  aaaZ
  aaa0
  aaa1
  aaa2
  aaa3
  aaa4
  aaa5
  aaa6
  aaa7
  aaa8
  aaa9
  aaa9
  aaa9
  aaa9
  </code></pre>