Solved

Parsing form data

Posted on 1998-09-04
1
133 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now