PDA

View Full Version : Basic PERL question


kf6rdn
05-19-2007, 06:52 AM
If you have a URL such as:

webserver/scripts/perlprog.pl?/storage/dir/a%20dir%20with%20spaces

Is there a function, perhaps in the CGI mod to convert that to:
/storage/dir/a dir with spaces from the QUERY_STRING env variable?

Or am I stuck regex parsing it out.

Tanks!

n2ize
05-19-2007, 10:00 AM
Quote[/b] (kf6rdn @ May 18 2007,23:52)]If you have a URL such as:

webserver/scripts/perlprog.pl?/storage/dir/a%20dir%20with%20spaces

Is there a function, perhaps in the CGI mod to convert that to:
/storage/dir/a dir with spaces from the QUERY_STRING env variable?

Or am I stuck regex parsing it out.

Tanks!
Take a look at...

CPAN.org (http://search.cpan.org/search?query=Query+String&mode=all)

particularly the first 2 modules listed. If no good a do a search. There are several Perl modules designed to parse query strings in a variety of ways. I just don't recall exactly which does what. Otherwise it should be pretty easy to capture the string and apply a Perl regexp.

kf6rdn
05-19-2007, 06:24 PM
Yeah I'd already searched the net, including cpan. In fact I spent so much time doing that, I probably coulda written the regex routine! doh!

I had come across the method when I was looking for something else. A'well maybe someone with more experience then you & I will come along and know it off the top of their head... Otherwise come Monday it's the long way...

n2ize
05-19-2007, 08:37 PM
I don't know if this is what you are looking for but, if your program captures the query string as you described with the actual space charachter representations "%20" embedded directly in the string you can use the Perl split and join functions as follows...

#!/usr/bin/perl

$query_string = "webserver/scripts/perlprog.pl?storage/dir/a%20dir%20with%20spaces";

@x = split(/\?/, $line);
@vals = split(/%20/, $x[1]);
$newline = join(" ", @vals);
print "$newline \n";

in which $newline will be "storage/dir/a dir with spaces"

I don't know if this is the end result you want but, it does work. You can tweak it (particularly the regexp's withing the split functions) to make it a bit slicker and foolproof. Just make sure it gracefully handles any curveballs. A Perl monger can probably do it in one single line that makes no sense to anyone but works. Perl is strange that way. Ya gotta love Larry Wall. #
http://www.qrz.com/iB_html/non-cgi/emoticons/biggrin.gif #http://www.qrz.com/iB_html/non-cgi/emoticons/biggrin.gif

kf6rdn
05-20-2007, 09:05 AM
Thanks, you didn't have to come up with all that.

Yeah, know what you mean about a perl monger, I had seen a 1 liner, possibly using the CGI mod in my "travels" but couldn't find it.
It's basicly an HREF link, if there's any spaces, they get translated to the %20. Actually any non standard character does. Fortunately it's an internal, limited to a few people so aside from an oddball directory popping up, it isnt subject to any hacking, aside from that of murphy and users that like to try things.

n2ize
05-20-2007, 06:58 PM
No prob... Instead of the "split" and "join" functions you could probably also use a Perl regexp directly along with the "substitute" and "global" flags that would turn it into a one line program similar to...

$line =~ s/<perl regexp>/g

but thats a bit more of a brain tease for me...Besides I don't think the split and join functions create much overhead and they're part of the base Perl installation so they should run on any server where Perl is installed.

Yeah, some Perl programmers pride themselves on their ability to condense several lines of code into one single line. Impressive ? yes. Nessesary ? no. Sometimes extremely hard to understand. Matter of fact Larry Wall's philosoply of Perl is that there is no one correct way to do any one thing and the more paths to a solution the better.

If you like Perl you might also enjoy using Python. It allows for similar flexibility. Several of my friends and co workers have reccomended Python to me. When I finish learning PHP maybe I'll take a look at Python.

KE7IPY
05-21-2007, 06:32 PM
use URI::Escape;

uri_escape($val)
uri_unescape($val)

These are your friends. uri_escape() will take a normal string and turn it into a web-safe escaped string, and uri_unescape will convert a string with the %20's and give you a regular string.

Of course, as is stated in the URI::Escape docs, uri_unescape() does the exact same thing as this Regex:

$string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;

kl7aj
05-21-2007, 06:42 PM
Quote[/b] (n2ize @ May 20 2007,11:58)]No prob... Instead of the "split" and "join" functions you could probably also use a Perl regexp directly along with the "substitute" and "global" flags that would turn it into a one line program similar to...

$line =~ s/<perl regexp>/g

but thats a bit more of a brain tease for me...Besides I don't think the split and join functions create much overhead and they're part of the base Perl installation so they should run on any server where Perl is installed.

Yeah, some Perl programmers pride themselves on their ability to condense several lines of code into one single line. Impressive ? yes. Nessesary ? no. #Sometimes extremely hard to understand. Matter of fact Larry Wall's philosoply of Perl is that there is no one correct way to do any one thing and the more paths to a solution the better.

If you like Perl you might also enjoy using Python. It allows for similar flexibility. Several of my friends and co workers have reccomended Python to me. When I finish learning PHP maybe I'll take a look at Python.
That's what I was thinking. Since % is such a common expression, you should be able to just substitute it with a null string

eric

n2ize
05-21-2007, 09:25 PM
Quote[/b] (KE7IPY @ May 21 2007,11:32)]use URI::Escape;

uri_escape($val)
uri_unescape($val)

These are your friends. uri_escape() will take a normal string and turn it into a web-safe escaped string, and uri_unescape will convert a string with the %20's and give you a regular string.

Of course, as is stated in the URI::Escape docs, uri_unescape() does the exact same thing as this Regex:

$string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
Yeah, the URI::* modules are a good place to look when confronted with this type of problem. However for the problem at hand I'd probably just use the regexp or, split and join with more robust regexp's in place.

KE7IPY
05-21-2007, 11:35 PM
The neat thing about this regex:

$string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;

is that it replaces any characters that are in HTML hex code (%20, %03, %11, etc) with the appropriate character. Neat.

n2ize
05-22-2007, 07:23 PM
The regexp is one of the more important tools that anyone who does any programming at all should have at her/his disposal. It's like a swiss army knife in your data parsing arsenal.

What makes Perl so neat is it has a very robust implementation of pattern matching regexp tools built into the base package.

Regular expressions have saved the day for me countless times. I recently got the O'Reilly book titled "Regular Expressions". Although I've managed to munge up a regexp when needed in a pinch I'd like to study the book and gain a solid comprehensive working knowledge or regexp's once and for all.

kf6rdn
05-23-2007, 12:20 AM
Thanks gang..
I settled on a variation that 'IPY had posted, utilizing the hex function in case someone comes up with something other then a %20 space char.

And yes, regex's are good to know, whether it's perl or egrep. Moving from a networking role to a sysadmin role has certainly been a learning experience. My programming experience was Turbo Pascal before. Nothings really similar to that, too bad I thought that a great language, with structure and readability, but low level enough to be fast.

I've been trying to decide between learning perl and php. Php seems to be quicker, at least for web apps I've seen, but perl seems more useful for command line stuff.
(Plus there's plenty of code out there to pillage routines from)

n2ize
05-23-2007, 08:07 PM
Quote[/b] (kf6rdn @ May 22 2007,17:20)]Thanks gang..
I settled on a variation that 'IPY had posted, utilizing the hex function in case someone comes up with something other then a %20 space char.

And yes, regex's are good to know, whether it's perl or egrep. #Moving from a networking role to a sysadmin role has certainly been a learning experience. #My programming experience was Turbo Pascal before. #Nothings really similar to that, too bad I thought that a great language, with structure and readability, but low level enough to be fast.

I've been trying to decide between learning perl and php. Php seems to be quicker, at least for web apps I've seen, but perl seems more useful for command line stuff.
(Plus there's plenty of code out there to pillage routines from)
PHP is nice in that (provided your server is set up to handle it) you can simply embed the PHP code right into your html using the <?php ... ?> tags...

<?php

$x = 0;
for ($x = 0; $x < 10; $x++) {
echo "<br>";
echo "Hello Ball !!";
}

?>

There is some controversy over which is more secure and of course there are Perl guys who will argue that PHP is garbage and vice versa...

I say whichever is best to do the job at hand. If I had a choice of knowing either one or the other it would have to be Perl over PHP. Perl is extremely robust, lends itself well to web apps, standalone apps, command line programs, and binds to several GUI frameworks. Not that PHP cannot do some of these things but I tend to find Perl more robust.

For ease of use in developing web apps PHP is great. The learning curve is slim , it's very dynamic, reasonably robust, and you'll be building all sorts of web applications in no time.

I'd also like to take some time and get into Python again. According to many of it's users Python is extremely dynamic and robust (more so than Perl according to many) and is quite efficiend in developing a broad spectrum of applications.. On the down side Python is not as widely used as Perl and may not have as broad a range of add on's... however I am not certain of the latter statement..

I'm still facinated with playing around with C and Assembly. It's pretty cool and impressive when you can directly open up an executable in a hex editor, modify a few opcodes, save it and change the behavior of the program without ever touching the source code, a compiler or an interpretor. Of course it can also be an easy way to wreck up a good, perfectly functional executable.



http://www.qrz.com/iB_html/non-cgi/emoticons/biggrin.gif http://www.qrz.com/iB_html/non-cgi/emoticons/biggrin.gif