![]() |
|
![]() |
||
![]() |
![]() |
![]() |
![]() |
![]() |
My Favorite Perl Functionsby Brent MichalskiDec. 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.
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.
chompPeople 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:
After this code is executed, $x contains This is a test$x = "This is a test\n"; chomp $x; chomp a list After this code is executed, $q contains a value of 3 and each variable has the last \n removed$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); chomp with no arguments After this code is executed, $_ contains the value This is another test$_ = "This is another test\n"; chomp; 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).
joinjoin 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: Results in $result storing This*is*a*test$w = "This"; $x = "is"; $y = "a"; $z = "test"; $result = join '*', $w, $x, $y, $z; Results in $result storing Brent*=*This*=*is*=*a*=*test*=*Michalski@array = ('This','is','a','test'); $x = "Brent"; $y = "Michalski"; $result = join '*=*', $x, @array, $y; 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.
pushpush 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: Results in @array2 storing Hockey Cards, Hockey Sticks, and Hockey Skates. Note that the punctuation (,.) is not stored, just the values.@array1 = ('Hockey Cards', 'Football Cards', 'Hockey Sticks', 'Hockey Skates'); foreach $item (@array1){ if($item =~ /hockey/i){ push @array2, $item; } } 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.
splitsplit, 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: This code snippet results in $d storing Brent, $e storing This, and @f storing is a test Michalski$x = "Brent*=*This*=*is*=*a*=*test*=*Michalski"; ($d, $e, @f) = split /\*=\*/, $x; 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.
unlinkunlink 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 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.$file_to_go = "/home/brent/thisfile.txt"; unlink ($file_to_go) || die "Error: $!\n"; With a list This segment deletes all files in the list @list, if they exist. Otherwise it sends an error message to the screen.@files_to_go = 'file1.txt','file2.txt','file3.txt'; unlink (@files_to_go) || die "Error: $!\n"; On an NT system
Deletes c:/inetpub/scripts/home/brent/thisfile.txt, if it exists. Otherwise it sends an error message to the screen.$file_to_go = "c:/home/brent/thisfile.txt"; unlink ($file_to_go) || die "Error: $!\n"; On NT systems, you must use the drive letter and full path of the file you want to work with. Wrapping it upThese 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 Techniques and Web Design and Development copyright © 1995-99 Miller Freeman, Inc. ALL RIGHTS RESERVED |