Solved

Parsing form data

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
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 …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

626 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