Solved

Parsing form data

Posted on 1998-09-04
1
143 Views
Last Modified: 2013-12-25
I have come across the following piece of code many times,when looking at CGI programs written in Perl. Can anyone explain to me exactly what is going on here?

$form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;

cheers
andy
0
Comment
Question by:AndrewThomason
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
bjt3 earned 200 total points
ID: 1828531
This perl takes input and modifies it as follows:

look for strings of the form %<hex digit><hex digit> and replace them with the corresponding
ASCII character.  For example %41 is "A".  So,

$form_data = "Here are some letters %41 %42 %43 ...\n";
$form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
print $form_data

prints out

Here are some letters A B C ...

Breaking this down, s/pattern/replace/options allows pattern replacement within a string.
=~ handles assignment.

the first pattern is %[\dA-Fa-f][\dA-Fa-f]  which means "percent sign followed by two hex
digits".   This is done by the two regular expressions [\dA-Fa-f] which match any single
character of 0-9 (from \d) and anything inthe range A-F and a-f.  Hence - any hex digit.

The replacement string is the pack function which takes the matching string (the hex number)
as an argument ($1) and generates the binary ASCII character.  (41 = "A").

The option eg says "evaluate the right side as an expression" and "replace globally across the
entire left string".  So, replace all matching elements based on this expression.

Does that help ?
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question