You have a bug:

map { s/.../.../seg; <-- Escapes the index. $form_data{$_} =~ ... <-- Should be using unescaped index. ... }

Not to mention that modifying $_ without localizing it is dangerous.

Fix:

$form_data = join '&', map { my $key = $_; my $val = $form_data{$_}; $key =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1)) +/seg; $val =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1)) +/seg; "$key=$val" } sort keys %form_data;

You should be using URI::Escape anyway:

use URI::Escape qw( uri_escape ); $form_data = join '&', map { my $key = uri_escape($_); my $val = uri_escape($form_data{$_}); "$key=$val" } sort keys %form_data;

Or even better yet, use URI::QueryParam:

use URI; use URI::QueryParam; my $uri = URI->new(...); $uri->query_param($_ => $form_data{$_}) foreach sort keys %form_data;

You have a second bug: The specs for this encoding (application/x-www-form-urlencoded) specifies that fields must be in the same order as the one in which they appeared in the HTML form. You're sorting them alphabetically instead.


In reply to Re: map { ; ; ; } @array by ikegami
in thread map { ; ; ; } @array by kwaping

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.