Forum Moderators: coopster & phranque

Message Too Old, No Replies

Learning Perl One Step at a time

Learning Perl One Step at a time

         

NemoV

3:58 am on Mar 19, 2005 (gmt 0)

10+ Year Member



Hi everyone,

I figure i would introduce myself a little before I started posting here since I have been lurking around for a few weeks. I am in the process of learning PERL and creating my own style of coding as apposed to using code snippets to get done what I want. I want to learn and write my own code and create my own scripts. I have been reading allot and writing lil scripts and my own snippets (to be put in a EditPlus Clip file).

Reverse engineering a script seems to be my manner of learning. I started out this way several years ago (97/8. By staring at the code long enough and toying with it till I understood the logic process. So if I wanted to do something I would look at a script that had those functions and copy them into a script that already running on my server/s. Yeah, this method of coding isn't really coding and prolly a longer process. But I wasn't doing this stuff for anyone else so I didn't care so long as I had the functionality I wanted or needed at the time.

I now understand the wisdom of writing ones own scripts and programs. I am in the process of rewritting a script to be a directory service of sorts. I have a couple parsing questions from a snippet in do not understand. Below is the snippet

# GET parse
my($incoming, @pairs, %FORM);
$incoming = $ENV{'QUERY_STRING'};
@pairs = split(/&/, $incoming);
foreach $pair (@pairs) {
my($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
The parts I do not understand is :

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

From what I can tell from this is I know it is searching for and then is to rewrite it pack it but what exactly is it searching for and what is it doing when is those items are found. any assistance or a direction for a tutorial to understand it would be greatly appreciated.

Have a wonderful day
NemoV

cgikira

4:30 am on Mar 19, 2005 (gmt 0)

10+ Year Member



$value =~ tr/+/ /;

This translates all instances of "+" in $value to a space.

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

This turns an encoded hex string like "%20" back into its appropriate ascii character value.

But, you're far better off ditching this old code and using the CGI.pm module instead, as it will decode the URL-encoding for you. Have a look at [cgi101.com...] for some examples.

-j.

Moby_Dim

4:48 am on Mar 19, 2005 (gmt 0)

10+ Year Member



Nothing bad in knowing the inner details.

NemoV

5:20 am on Mar 19, 2005 (gmt 0)

10+ Year Member



Wow what a quick response. Thanks!

So I take it the tr/ means Translate I knew s/ means search and replace Seems to me tr/ and s/ are basically the same.

My uneducated thoughts Inreference to using modules, I could be way off and if so please by all means tell me.

In many cases they are preferred and an excellent method of short cutting sometimes thousands of lines. but I have a preference for not using modules for something that only takes a few lines or I have a snippet doing the same thing. Now some people may argue that the script would run faster because the module is loaded by calling it I think that is dependant on what is being called from the module being loaded in the process, if it's a a 300k module and you only need to write 10-25 lines of code I think it would be less of load on the server for the process. And I truely like knowing all processes that are being run and if you call a module: it creates a Blackbox effect you know it (the module) will do what you want but you don't know how it is doing it and what else it could be doing at the same time. There is also the idea of portablity, I have domains on different servers and hosts The server set up is different for each one like one server uses cgi wrapper and the other one doesn't but the one that has wrapper has several modules on it but the one that does not use cgi wrapper has very few modules.

Thanks again, I am off to go check out the link posted here

Have a wonderful day

NemoV

NemoV

6:08 am on Mar 19, 2005 (gmt 0)

10+ Year Member



after checking out cgi101.com, I am rethinking the non use of modules.

coopster

3:38 pm on Mar 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, NemoV.

Your thoughts on modules are not far off. Yes, modules are good, very good, and quite useful in many senses. If you have the time and energy, go ahead and roll your own code. If you want to use a module and understand how it works, then use it! It's always, always going to be a judgement call. MobyDim makes a great statement here in that there is ...


Nothing bad in knowing the inner details

Truer words have never been spoken. I asked Lincoln Stein once regarding his cgi.pm module what he thought about the criticism some folks throw at it regarding "code bloat", etc. His response was much the same as what we are discussing here. If you need the structure, use it. If not, take what you do need and make your own. Times change and each application calls for different analysis and design.

I remember struggling with the same issues when I first delved into perl. Don't worry, whether you use a module or not, it will always be there since you can implement your own version of any of them.

BTW, tr/// is more commonly known as transliteration [search.cpan.org], but can be used as translation[/url]. There is a synonym for this operator as well, it is y/// (a provisional carry-over by Larry Wall for the 'sed' folks).

NemoV

10:02 pm on Mar 19, 2005 (gmt 0)

10+ Year Member



Thanks Coopster, glad to know my thinking isn't too far off. I guess, when it comes to the use of modules it may be more of style, time and funtionality (not exactly in that order).

I can see the reason for creating modules for scripts and programs that are not "free" GPL'd or Open sourced as it hides some of the functionality and to make changes to the program you have to edit the module. Requiring a specific module installed on a server to run a simple routine for the script to work. the reduction of theft / infringement would prolly only go down by a small percentage considering the average for Digital Rights infringe and theft from websites I think is around 20%, but that is an entirely different topic.

Now as far as learning the inner details, I look at it this way. I want to be a programmer / web developer. I LOVE figuring out the logic of ZXY to do XYZ. I want to beable to go from using other scripts that are functioning and customize them to my own needs to writing my own processes and syntax. The only way for me to know this stuff is reading studying, trial and error along with talking with other programmers and webmasters. Without learning the details of PERL the actual syntax, I will be like a handyman electrician who really only knows how to install light fixtures and ceiling fans in a house that already has the electrical wiring done. One doesn't need to know how elcetricity works in order to use it. :-) but if you want to know how to wire a house one should ...

I will be a frequent poster to this forum
Have a wonderful day

NemoV

rocknbil

6:21 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if I agree 100% on using a module in every instance possible.

The read/parse routine in the example is what, 20 lines of code? If all you're doing is reading and parsing simple form data that's all you need. The CGI.pm module is several hundred lines long. I rarely call it in unless I need to do heavy duty stuff, such as multipart form data for uploads.

The other problem I've found is many modules **almost** do what I want but not quite. If I'm writing 100 lines of code to reformat a difficult module output, I begin to think I should ditch the module and write it from scratch. :-D

volatilegx

7:31 pm on Mar 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like modules for keeping code-bloat to a minumum. However, because I develop scripts for mass-distribution, portability is an important issue. The scripts I write only use modules that are widely distributed.

SeanW

8:08 pm on Mar 21, 2005 (gmt 0)

10+ Year Member



CGI.pm's methods are mostly autoloaded, so you're not really bloating your app by using it just for url decoding.

If you want to preload all the methods (ie in your httpd startup script if you're using mod_perl), use

[perl]
use CGI qw(:all);
[/perl]

Sean

NemoV

2:11 am on Mar 22, 2005 (gmt 0)

10+ Year Member



So, how would I update / create different versions of my scripts from the antiquated way of parsing data to using the CGI mod
for Example

sub post_parse {
my(@pairs);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
foreach $pair (@pairs) {
my($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.¦\n)*-->//g;#remove SSI
$FORM{$name} = $value;
}
}

Would I change it to


sub post_parse {
my(@pairs);
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
my %FORM;
foreach my $field (param()) {
$FORM{$field} = param($field);
}
}

Now to me (no formal training in programming )this seem too obscure, magick like. I want to understand and know how the details work. CGIKIRA gave me a nice link on some of this but I want more I am PAC MAN and I want more dots any suggestions on where I can go to learn more about using the CGI mod if it's widely distributed I may convert some of my scripts to it use it.

Funny I have been doing my best to work smarter and not harder over the past two years and when ever I learn to speed up a process from submitting pages manually or creating webpages / sites by hand to having a script do the work for me. I end up starting more projects, I may be working "smarter" but I am still working just as long. Having the knowledge to do something rarely compares with the fullfillment that follows from working towards and accomplishing a Vision from the knowledge one has for a particular thing.

Have a Wonderful Day

NemoV

cgikira

3:47 pm on Mar 22, 2005 (gmt 0)

10+ Year Member



Actually to replace the old form-parse code, you'd just use:

use CGI qw(:standard);
my %FORM;
foreach my $field (param()) {
$FORM{$field} = param($field);
}

Or for even fewer lines of code, use:


use CGI qw(:standard Vars);
my %FORM = Vars();

-j.

timster

7:23 pm on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seems to me tr/ and s/ are basically the same.

They're similar tools but they're pretty distinct once you get into it.

There's a lot to regular expressions; you might consider learning regex as a "separate project" in addition to learning Perl. (There are whole books on regex, anyway, e.g., "Mastering Regular Expressions.") Just Google "Perl regular expressions" for some good tutorials.