blank.gif
webreview.com - Cross-Training for Web Teams
Search for: 
Jump to:
blank.gif
blank.gif

 
 

A Songline PACE Production



My Favorite Perl Functions

by Brent Michalski
Dec. 4, 1998 

With my frequent visits to the Perl newsgroups, I see many of the same questions asked over and over. I know that we have great Perl FAQs, but there are many times that if the programmers were just more familiar with Perl's basic functions, they would not need to ask the questions.

This article is serving a twofold purpose.

  1. The more Perl functions that you are familiar with, the better Perl programmer you'll be.
  2. I was writing an article which describes how to create on-line quizzes or tests, but I ran into a few snags - so it will have to wait until next time.
While I won't have a demonstration program this week, I will show some code-snippets to explain how the functions work.

I picked the Perl functions that are very useful for many different applications and that I like to use often. I'll be covering chomp, join, push, split, and unlink. These are all very useful and often underused functions.

chomp 

People often overlook chomp and use chop instead. 

The purpose of chop is to remove the last character of a string. Many times this is what a programmer wants to do, but chop can be too indiscriminate. chop will remove the last character of the string, no matter what the character is.

chomp, on the other hand, [get it? ;^) ] is much more selective about what it takes off the end of a string. chomp still removes the last character of a string, but only if it equals the current value of $/. $/ is the Input Record Separator, by default it is set to the newline character (\n).

chomp can work on a variable, list, or if nothing is specified -- the special variable called $_

Examples:
chomp a variable

$x = "This is a test\n";
chomp $x;
After this code is executed, $x contains This is a test

chomp a list

$x = "This is a test\n";
$y = "This is a test\n\n";
$z = "This is a test\n\n\n";
$q = chomp ($x,$y,$z);
After this code is executed, $q contains a value of 3 and each variable has the last \n removed

chomp with no arguments

$_ = "This is another test\n";
chomp;
After this code is executed, $_ contains the value This is another test

In all of the examples above, we could have set a variable to store the return value if we needed to use it (like we did with $q for $y). 

join 

join is a great function. join allows you to join a list together with a common field separator. join is perfect for working with flat-text files used as databases.

join is the opposite of the split function, which we'll discuss in a minute.

Examples:

$w = "This";
$x = "is";
$y = "a";
$z = "test";

$result = join '*', $w, $x, $y, $z;
Results in $result storing This*is*a*test
@array = ('This','is','a','test');
$x = "Brent";
$y = "Michalski";

$result = join '*=*', $x, @array, $y;
Results in $result storing Brent*=*This*=*is*=*a*=*test*=*Michalski

As you can see, you can use more than one character as the field separator. Also notice that you can pass an array to join and all elements in the array are joined together with the field separator.

push 

push is a useful function for working with arrays. push can also be used for more complex things like creating stacks but I am going to stick with the simple description here. For a more detailed description, check your Perl documentation.

I use push very often when working with arrays. push allows me to add an item onto the end of the array very easily.

Example:

@array1 = ('Hockey Cards',
           'Football Cards',
           'Hockey Sticks',
           'Hockey Skates');

foreach $item (@array1){
  if($item =~ /hockey/i){
    push @array2, $item;
  }
}
Results in @array2 storing Hockey Cards, Hockey Sticks, and Hockey Skates. Note that the punctuation (,.) is not stored, just the values.

As you can see, push is really an easy way to add items onto the end of an array. I use push very often when programming in Perl, especially when I am searching through data files.

split 

split, as we mentioned earlier, is the opposite of the join function.

split also has many uses. I use split frequently when I work with flat-file databases. split allows me to go through each record and split it into it's respective fields.

Example:

$x = "Brent*=*This*=*is*=*a*=*test*=*Michalski";

($d, $e, @f) = split /\*=\*/, $x;
This code snippet results in $d storing Brent, $e storing This, and @f storing is a test Michalski

Notice that I put the variables first and the array last. If I had tried to split the fields with @f listed first, and $d and $e last, all of the data would get stored in @f and nothing would ever make it to $d and $e. You can have both variables and lists/arrays come out of the split function, just make sure that the arrays always come last.

unlink 

unlink is probably the function that gets missed by most Perl beginners. unlink allows you to delete files on the system. You would think that to delete files, you would use a function called "delete", or something similar. But thats not the case.

unlink is another function which is very simple to use. To make your code easier to debug and much more reliable, we'll use an or die statement to verify that our operation completed.

Examples:

On a Unix system

$file_to_go = "/home/brent/thisfile.txt";
unlink ($file_to_go) || die "Error: $!\n";
This code segment deletes the file /home/brent/thisfile.txt if it exists. Otherwise it sends an error message (Error: plus the system error it recieves) to the screen.

With a list

@files_to_go = 'file1.txt','file2.txt','file3.txt';
unlink (@files_to_go) || die "Error: $!\n";
This segment deletes all files in the list @list, if they exist. Otherwise it sends an error message to the screen.

On an NT system
 

$file_to_go = "c:/home/brent/thisfile.txt";
unlink ($file_to_go) || die "Error: $!\n";
Deletes c:/inetpub/scripts/home/brent/thisfile.txt, if it exists. Otherwise it sends an error message to the screen.

On NT systems, you must use the drive letter and full path of the file you want to work with. 

Wrapping it up 

These functions are just a few of my favorites functions that I use very often. As you become more comfortable with Perl, you will start using more and more functions. Don't be afraid to try out a new function, it may be just what you are looking for!

I remember when I didn't know about the join function I was hand coding all of the things that join did for me. One day, I picked up my copy of Programming Perl and was just browsing through the chapter on functions and I came across it. I haven't stopped using it since!

Next: An Online Quizzer

Web Review copyright © 1995-99 Songline Studios, Inc.
Web Techniques and Web Design and Development copyright © 1995-99 Miller Freeman, Inc.
ALL RIGHTS RESERVED