Home arrow Perl Programming arrow Page 8 - String Processing with Perl

Desperately Seeking Susan - Perl

Perl has always been known for its text processing and manipulation abilities. This article examines the Perl string handling API in greater detail, explaining how you can use Perl's string functions to (among other things) print and format strings, split and join string values, alter string case, and perform regular expression searches.

  1. String Processing with Perl
  2. Jumping Jacks
  3. Choppy Waters
  4. Making New Friends
  5. Not My Type
  6. Of Jumping Cows And Purple Pumpkins
  7. On The Case
  8. Desperately Seeking Susan
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 76
April 16, 2003

print this article


You can also search for specific patterns in your strings with regular expressions, something that Perl supports better than most other languages.

In Perl, all interaction with regular expressions takes place via an equality operator, represented by =~

$flag =~ m/susan/
$flag returns true if $flag contains "susan" using the "m" operator.

You can also perform string substitution with regular expressions with the "s" operator, as in the following exanple.

$flag =~ s/susan/JANE/
This replaces "susan" in the variable $flag with "JANE" using the "s" operator.

Here is a simple example that validates an email address:

#!/usr/bin/perl # get input print "So what's your email address, anyway?\n"; $email = ; chomp($email); # match and display result if($email =~ /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/) { print("Ummmmm....that sounds good!\n"); } else { print("Hey - who do you think you're kidding?\n"); }
Obviously, this is simply an illustrative example - if you're planning to use it on your Web site, you need to refine it a bit. You have been warned!

If you want to find out the number of times a particular pattern has been repeated in a string, Perl offers the very cool "tr" operator.

#!/usr/bin/perl # get input print "Gimme a string: "; $string = ; chomp($string); # put string into default variable for tr $_ = $string; # check string for spaces and print result $blanks += tr/ / /; print ("There are $blanks blank characters in \"$string\".");
Here's an example session:

Gimme a string: This is a test. There are 3 blank characters in "This is a test.".
You can have Perl return the position of the last match in a string with the pos() function,

#!/usr/bin/perl $string = "The name's Bond, James Bond"; # search for the character d $string =~ /d/g; # returns 15 print pos($string);
and automatically quote special characters with backslashes with the quotemeta() function.

#!/usr/bin/perl $string = "#@!#@!#@!"; $string = quotemeta($string); # returns \#\@\!\#\@\!\#\@\! print $string;
Sadly, that's about all we have time for. In case you want more, consider visiting the following links:

The Perl string API, at http://www.perldoc.com/perl5.6/pod/perlfunc.html

The Perl 101 series, at http://www.devshed.com/c/a/Perl/Perl-101-Part-1--The-Basics/

A discussion of regular expressions, at So What's A $#!%% Regular Expression, Anyway?!

Until next time...be good.

Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

>>> More Perl Programming Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: