Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Perl 101 (part 6): The Perl Toolbox
Review some powerful built-in Perl functions for string and math operations.

| Expressing Yourself |

One of Perl's most powerful features is the ability to do weird and wonderful things with "regular expressions". To the uninitiated, regular expressions, or "regex", are patterns which are built using a set of special characters; these patterns can then be compared with text in a file, or data entered into a Web form. A pattern match can trigger some kind of action...or not, as the case may be.

Though regular expressions can get a little confusing when you're first starting out with them, a little patience will reap rich rewards, as they can save you a tremendous amount of work. Our very first example illustrates a simple pattern-matching operation, and introduces you to Perl's match operator:


#!/usr/bin/perl

# get a line of input
print "Gimme a line!\n";
$line = <STDIN>;
chomp ($line);

# get a search term
print "Gimme the string to match!\n";
$term = <STDIN>;
chomp ($term);

# check for match
if ($line =~ /$term/)
{
print "Match found!\n";
}
else
{
print "No match found\n";
}


A quick explanation is in order here. In Perl, the "pattern" is the sequence of characters to be matched - this pattern is usually enclosed within a pair of slashes. For example,


/xyz/


represents the pattern "xyz".

The example above asks for a line of text and a search term - this search pattern is then used with the match operator =~ to test for a match. The result of the =~ operation is true if the pattern is found in the string, and false if not.

Here's the output of the example above:


#!/usr/bin/perl

# get a line of input
print "Gimme a line!\n";
$line = <STDIN>;
chomp ($line);

# get a search term
print "Gimme the string to match!\n";
$term = <STDIN>;
chomp ($term);

# check for match
if ($line =~ /$term/)
{
print "Match found!\n";
}
else
{
print "No match found\n";
}


Perl also has the !~ operator, which does the reverse of the =~ operator - it returns true if a match is NOT found.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback