Perl/regexp help... - query string parsing...
I have the unlucky situation of having taken over control at a small company who's CTO quit. I'm a single programmer (im a 20 year old comp sci student!) and I have this massive, undocumented code base to figure out. Fun job for anyone, I know :(
Anyways, I'm looking at this one particular webpage I'm forced to change. I'm really not that good at Perl compared to how I am in other languages. So I'm trying to figure out what the old programmer was doing to break apart this query string. If someone could explain this little code block I would really appreciate it. The same block or similiar blocks are all over the site, it must be on 20 pages. If it makes a difference I've kinda mustered out that we're using mod_perl and/or mason. But we still use a regular shabang line :confused:... Anyways some lines/pieces I understand completely, others I have NO clue about. Please read my comments... Code:
use URI::Escape; Thanks :) |
Code:
read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); Code:
$Value =~ tr/+/ /; Code:
$Value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg Code:
$encshname =~ s/'/%27/g; |
It's not too dificult if you know a little about HTTP.
Thinking in CGI: When you request /cgi-bin/page.pl?a=1&b=2 you do a GET request. The parameters are in the url. But you can request /cgi-bin/page.pl and stream a=1 later. Then you do POST. You can imagine the browser telling this to the web server: POST /cgi-bin/page.pl HTTP/1.1 agent: foo accept: bar a=1&b=2 And then you read this via stdin. OK? that's easy Now come the +. A url can't have spaces inside, so what do you do when you want to send one? encode it as '+'. And when you want to send a '+'? encode it as %2B. Yeah, it's really fun. foo bar: foo+bar foo+bar: foo%2Bbar |
I forgot the:
s/%([\dA-Fa-f][\dA-Fa-f])/pack("C", hex($1))/eg Do you remember the + as %2B? sure. 2B is the '+' char ASCII hex value. %([\dA-Fa-f][\dA-Fa-f] is the regex of a hexadeciaml numebr. Sure you see it if I rewrite as: [0-9A-Fa-f][0-9A-Fa-f]) The \d means decimal. Now be a good engy RTFM about pack(). perldoc is your friend. |
Re: Perl/regexp help... - query string parsing...
Code:
use URI::Escape; Hope this helps |
All times are GMT -5. The time now is 10:35 PM. |