The basic Perl filter: Comments

  1. Splitting and printing
  2. The basic Perl filter

I mentioned earlier that the pound sign at the beginning of a line causes Perl to completely ignore that line. This makes the pound sign a useful way of adding comments to your scripts. Comments are very important: they help you remember what you meant by this snippet of script several months or even years later when you look at the script again.

This script, for example, might be commented as follows:

#!/usr/bin/perl
#Search for songs in a file of the following tab-separated data:
# title, duration, artist, album, year, rating, rip date, track position, genre

#the first item on the command line is what we're searching for
$searchFor = shift;
while (<>) {
#split out the song, duration, artist, and album from the current line
($song, $duration, $artist, $album) = split("\t");

#print song information if this line contains our search text
print "$song ($album, by $artist)\n" if /$searchFor/i;
}

You don’t need to comment every line, but it is a good idea to comment every section. You’ll usually want to put a comment in front of any while block, or other large block of Perl lines.

  1. Splitting and printing
  2. The basic Perl filter