This is some decoding script for form input. You should not be doing this yourself. See Use CGI or die; and No excuses about not using CGI.pm

Anyway here is an explanation:

# iterate over @pairs array assigning each value to $pair in turn
# this should start foreach my $pair (@pairs) 
# see Use strict warnings and diagnostics

foreach $pair (@pairs){

# this splits each pair into $name and $value at the "="
# the "=" is thrown away

      ($name,$value) = split(/=/,$pair);

# this tr(ansliterates) the "+" chars into spaces
# in HTTP this is how spaces are encoded

      $value =~ tr/+/ /;

# this s(ubstitutes) some special encoded characters into their ASCII values
# In HTTP certain chars have special meaning to the protocol so need
# to be encoded. These are encoded as their hex equivalent
# The encoded form is %DD where the DD are two hex digits and
# the leading % is a flag to let us know the next two chars are 
# hex digits encoding a ASCII literal. Now to the regex itself.
# the first part captures the two hex digits after the % into $1
# the second part converts these to their ASCII equivalents
# the /eg at the end means "e" evaluate the second part and 
# "g" do globally ie all ocurrences

      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
      

# this last line generates a associative array (a hash) called %VALUE
# the key in this hash is $name and the value is $value

      $VALUE{$name} = $value;

I recommend you check out Ovid's Web Programming with Perl tutorial for more details. This code will choke on names like "Jack&Jill" or "John Smith" because the decoding is only performed on $value, not $name. These will remain encoded as "Jack%26Jill" and "John+Smith".

cheers

tachyon


In reply to Re: Enlightment for a little french by tachyon
in thread Please explain this (CGI) parsing routine by Prosélyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.