The answer which no one is willing to give you is this:
my %pairs = split(/&|=/, $buffer);
A hash can accept an ordered list of pairs which it will assign using a KEY VAL KEY VAL scheme.
NOTE: I am only providing this answer because your method can be improved if you insist on ignoring the sound advice to use the CGI module for handling HTTP transactions. This sort of paired-list-to-hash assignment is very useful in creating arguments for subroutines, see
here for more information.
Update:This post was made in the spirit of solving the problem as stated, and is a great example of why using the CGI module is better than recoding parts of it. While this solution will work for the stated problem involving key/value pairs, it will have serious problems (as pointed out below) when you have checkboxes or empty (no default values) form fields.
It will also likely break as soon as you change your form, unless you are overly restrictive in your form construction (and prohibit check boxes and enforce default values for all fields). All of this is an inefficient use of your time as a coder. Especially when working with CGI and web development, you open up a Pandora's box of problems for yourself or your clients if you do not take advantage of every well-tested solution (like CGI.pm) and get a solid understanding of the programmming you are doing before you build something that is so easily exposed for attack (that is, the web site).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.