[Xitami] Off Topic Perl Programmer

Thomas J. Hruska xitami@lists.xitami.org
Fri, 13 Jul 2001 18:19:36 -0500


At 12:23 PM 7/13/01 -0700, you wrote:
>Hi All,
>
>I need someone to take a perl script I have and make it so that
>I can pass form values to it. I know just about nothing when it
>comes to perl thats why I was looking for someone to help.
>
>Anyone able to offer a hand? It's very very simple. I can do it
>in cold fusion but not in Perl. Thanks,
>
>david

The following two functions will extract each value from STDIN (where form
values are placed) and put them into a hash.  Just use '&ParseForm();'
(without the quotes) to parse the data into the 'values' hash, then you
access your form variables using '$values{FormName}' to access each
individual element of the form data.

sub ParseForm
{
  local ($fields, $name, $value, $data);

  read(STDIN, $data, $ENV{"CONTENT_LENGTH"});
  @fields = split(/&/, $data);

  foreach $item (@fields)
  {
    ($name, $value) = split(/=/, $item);
    $name = &UnescapeString($name);
    $value = &UnescapeString($value);
    $values{$name} = $value;
  }
}

sub UnescapeString
{
  local ($s) = $_[0];
  local ($pos, $ascii);

  $s =~ s/\+/ /g;       # Replace the + sign with spaces

  $pos = 0;             # Replace %xx with hexadecimal
  while (($pos = index($s, "%", $pos)) != -1)
  {
    $ascii = hex(substr($s, $pos + 1, 2));
    substr($s, $pos, 3) = pack("c", $ascii);
  }
  $s;
}


Hope that helps!


           Thomas J. Hruska -- shinelight@crosswinds.net
Shining Light Productions -- "Meeting the needs of fellow programmers"
                  http://www.shininglightpro.com/