Solved

Parsing form data

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

705 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

18 Experts available now in Live!

Get 1:1 Help Now