Solved

Parsing form data

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help to rewrite script 3 79
Scripted configuration of TightVNC with Start on boot 7 59
AutoHotkey ActiveX Component Error 8 85
How can i open a .fxp file 3 31
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

770 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