Electric Type

Multimedia

About Us

News

Help

Intro to Perl for CGI

Page 8 — My Baby's All Grown Up!

Great, you've almost got it. Just look at you! My little Perl Jedi! Of course, even a Jedi needs a few weapons. Here's a few extra things to help you write the perfect Perl script to master the Force and all that garbage.

Perl Split, Please

Splitting is a function that will save you lots of time and money (well, time, at least). It looks for a pattern in a string and splits up the string into an array based on the pattern. Have a look.


#!/usr/local/bin/perl



# I have a string.

$stuff = "Colin is so cool";



# I decide that I want to split it up by spaces into an array.

@stuff = split (/ /, $stuff);



# I then want to print out each slot on a different line.

for ($i = 0; $i <= $#stuff; $i++) {

	print "Slot $i: $stuff[$i]\n";

}



# Find out how many words there were like this.

print "There were $#stuff words.\n";

Play around with this tool a little bit, and you'll find out just how useful it really is.

Search and Destroy

If you always misspell the same words as I do, then you'll love Search and Replace. Here's how it works: Place =~ s/searchstring/replacestring/gi; after the string you want to run Search and Replace on, replacing searchstring and replacestring with the items you want searched and replaced, like so:


$stuff =~ s/bad/bad bad leroy brown/gi;

So if I wanted to fix all my "e"s before "i"s, I might do something like this:

# I have this string:

$stuff = "beleive releive greive";



# I want to fix all the mistakes.

$stuff =~ s/ei/ie/gi;



# Then I want to print out the output.

print "$stuff";

The niftiest part is that you have a couple of different options with this tool. At the end of statement, there are two letters: "g" and "i." Those are two options: "g" tells the command to search and replace all occurrences of the string, and "i" tells it to ignore case. One other cool option is the "o" option, which only replaces the first occurrence. (It's the opposite of the "g" option.) You can use one, all, or none of these options with the command.

Last, you can even use it to tell if something is in a string. Say that you wanted to do something only if the string contained the word "cool." The hard way to do this is to split up the string and run a loop through the array, looking for the word. Thankfully, there's any easier way: a quick search.


$stuff =~ /cool/

This statement would return a true value if the string contains the word "cool" and return a false value if it didn't. You can also do the reverse:

$stuff != /cool/

... which would return a true if the string doesn't contain the word "cool" and a false if it does. Then you can use it in an if statement like so:

if ($stuff =~ /cool/) {

	statements

}

All kinds of niftiness!


So you're still here. That's got to say something. Either you're waiting for my phone number so you can ask me out on a date, or you're so enthralled by this whole Perl thing that you're unable to move. I hate to tell you, kid, but if it's the second, then welcome to the world of lost sex appeal, crack, caffeine, nicotine, and sleep deprivation. If it's the first, just email me, babe.

Now that I've given you a taste of Perl, no doubt you're probably jonesing to actually do something with it. Read on to learn how to turn Perl into CGI.

next page»


Dynamic HTML  

Frames  

HTML Basics  

Stylesheets  

Tables  

XML  

Javascript  

Database Connections  

Intro To Perl  

HTML 4.0  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.