Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

A regexp from paypal

by davidov0009 (Scribe)
on Nov 25, 2007 at 20:45 UTC ( [id://652858]=perlquestion: print w/replies, xml ) Need Help??

davidov0009 has asked for the wisdom of the Perl Monks concerning the following question:

I am not at all prepared to deal with regular expressions so when this one came up in a paypal script I found myself unable to find out what it does. Any help appreciated.

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

It is used on the value of a POST from the paypal webserver when they are being read into a hash

Also, if I have a script where should I post so that others can access it (source code wise, not execution).

Replies are listed 'Best First'.
Re: A regexp from paypal
by renodino (Curate) on Nov 25, 2007 at 20:52 UTC
    Its just URI unescaping, i.e., converting "%21" back into '!', "%22" back into '"', etc.

    Perl Contrarian & SQL fanboy
Re: A regexp from paypal
by Corion (Patriarch) on Nov 25, 2007 at 20:55 UTC

    You can post this script as a reply to your root node. Please wrap it in <code>...</code> tags so it renders and downloads nicely.

    If the script is hand-rolling its own parameter decoding, it has likely other deficiencies. Normally, you want to use CGI.pm for handling all the CGI decoding.

      That is the funny thing, I am adapting a script put out by PayPal that had their own handrolled CGI and attempting to make it use the CGI module.

      The problem is I need to get a just the straight post string to add a parameter and send it off, param() isn't letting me do it. Any ideas on how to access the raw post string using the CGI module, right now the script does this:

      read (STDIN, $query, $ENV{'CONTENT_LENGTH'}); $query .= '&cmd=_notify-validate';
      There has to be something in CGI to do that read() line.
        Since CGI.pm gives you a couple of ways to fetch all the parameters it's passed, you could just add your new parameter to the list and then send it all back out using LWP.

        With a little bit of error checking, this could also save you from blindly sending back out any malformed data you receive.

Re: A regexp from paypal
by bart (Canon) on Nov 26, 2007 at 07:51 UTC
    It looks like something lifted directly out of cgi-lib.pl, which is, well, ancient. It really looks like some beginner programmer wrote his own CGI parameter processing code, borrowing here and there... (And, haven't we all...) There really is no need to use pack "C", ... any more, as Perl 5, which has been around for more than 12 years, has chr. (Perl 4 did not.)

    So: you might use this code to learn some Perl, but please don't copy it, as it can really not be considered "best practices" any more.

      Yep I know that, just need to know what it did so that when I was rewriting this program to use CGI that I didn't leave something out. I've gotten past that now and am having trouble implementing CGI.pm to send back the post request to the server. When I post the query object back PayPal doesn't like the way it's formatted. I am trying to resist using the read() function to get the post string but the CGI module isn't making this too easy. Does anyone know how I can get at the post data and add a var (I've done this using param() ) and then send it back using LWP without screwing up the format?
      use strict; use CGI;
Re: A regexp from paypal
by Anonymous Monk on Nov 26, 2007 at 07:38 UTC
    Google says http://www.cgi101.com/class/ch4/text.html
    Next is a rather complicated regular expression that substitutes every %HH hex pair back to its equivalent ASCII character, using the pack() function. We'll learn exactly how this works in Chapter 14, but for now we'll just use it to parse the form data:
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://652858]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found