Perl Commands

Home

Perl path

It is good programming practice to tell a Perl program where to find the Perl program. On most servers, this is in the directory /usr/bin/perl, although you will probably have installed it in C:\Perl. At the top of your program (i.e. on the first line, or it will be interpreted as a comment) insert:

#!/usr/bin/perl

To find out where Perl is, type $ whence -v perl in the Korn or Bourne shell in Unix, or $ which perl in the C shell. Otherwise, try $ find -name perl -print 2>/dev/null.

Printing

The tag for printing is 'print', the text to be printed is within quotes, and the statement is terminated with a semicolon, for example:

print "Hello world!";

Variables

Perl supports five variable types (as opposed to data types), distinguished by their first character. The second character may be a letter or an underscore ( _ ). The remaining characters may be alphanumerics or underscores. Variable names are case sensitive. The variable types are:

Rewrite with better descriptions, examples.

Variable typeDescriptionExample
ArrayAn indexed list. @myarray = (2,4,6,8);
HashAn associative array, keyed by strings. %country("Bill") = "USA";
ScalarA variable which holds a single value. $myvariable = "mine all mine";
SubroutineA callable section of a Perl program &displaytable("league");
TypeglobEverything named dog. *dog;

If statements

The format is if (comparison) then {action}. For example:

if ($myvariable eq "mine all mine") { print "You're not having it!"; }

Input

$input = ;
If $input ne "" {
print ($input);
}

Comments

All comments start with a hash character and continue to the end of the line. Note, however, that the path statement, which can only appear on the first line, also begins with a hash. Comments can not be inserted in the middle of a statement, but can go on the end of the statement.

#!/usr/bin/perl
# the line above is not a comment
$myvariable = "mine all mine"; # but this is a comment following an assignment

UNDER CONSTRUCTION

Also add Perl notes to explain the following examples:

$my_percent = substr((($my_total/$total_referrals)*100),0,5);
$my_total = ($my_total + $number);
$number++;
$source_url = "Direct Hit";
$url = "1 $source_url";
&print_table;
($number,$url) = split(/ /,$_,2);
($number,$url) = split(/ /,$line,2);
($number,$url) = split(/ /,$refer,2);
@array = sort descending @array;
@array=@my_url;
@referrals = <refer_file>;
@search_engine_ids = ("altavista", "yahoo", "lycos", "webcrawler", "excite", "infoseek", "metacrawler", "hotbot", "mckinley");
@sources = <source_file>;
close(source_file);
flock(source_file,2); flock(source_file,8);
for ($x=0; $x < $report_size; $x++)
foreach $_(@referrals) foreach $engine(@search_engine_ids) foreach $line(@array)
foreach $refer (@sources)
if ((/$engine/) || (/=/))
open(source_file,"$source_file"); open(source_file,">$source_file");
print source_file "$refer\n";
print "<a name=\"search\"></a>\n";
print "<form method=\"post\" action=\"../cgi/clear.pl\">";
print "<input type=\"submit\" value=\"submit\">";
print "<input type=\"text\" name=\"l\">";
print "<tr><td>$number<td>$percentage%<td><a href=\"$url\">$url\n";
print "Content-Type: text/html\n\n";
push (@sources,$url); push(@my_url,$_);
sub print_table
unless ($found)

@array = sort descending @array;
sub descending
{ if ($a < $b) { 1; }
elsif ($a == $b) { 0; }
elsif ($a > $b) { -1; }
}

Back to top     Home     Serious Stuff

© 2001. Copyright Sue Nethercott.