Variables: Perl does not require that variables be declared, but it does have some syntax rules. Variable names begin with a dollar sign and then text. Arrays begin with @ and then text. There is another sort of variable called a hash-array and these begin with a % and then text.

Perl also has pointers - putting a \ in front of a variable is a pointer to it:
\$xx, \@xx and \%xx
are all pointers, the first to the variable $xx, the second to the array @xx and the third to the hashed array %xx. Exactly what is in a variable such as $xx is interpreted by context. This takes some getting used to but can be very powerful. When $xx is numerical, the usual C increment, decrement and assignment operators are available. Additionally Perl supports assignment operators for text: $xx.=" moose"; appends a space and then moose to the text in $xx. From this you might guess (correctly) that the binary operator . denotes string concatenation. Here is a simple program to count sheep:

$i=1;
while($i<100) {   # This is a Perl comment.
  print $i++." sheep.\n";
  }


Files: Files are handled by the user through file names. Files must be opened before use and an appropriate file structure initialized. This is accomplished using an open statement for files (or an opendir statement for directories). They both take two arguments and the first is just a name for the file structure.
open(FILE,"file_name")
opens the file named file_name for read only. open(FILE,">file_name")
open(FILE,">>file_name")
opens the same file for writing or for appending. If the file does not exist, it is created. Finally open(FILE,"+>file_name")
opens the file for both reading and writing.
The opendir command works similarly except that you can only read a directory
opendir(DIR,"dir_name");
To create a directory, use the command
mkdir("dir_name",777);
where you can replace the 777 by other modes as you wish, although be warned that under AFS, UNIX permissions are irrelevant. There are low-level read and write statements, but one can easily read small files into memory with the command
@lines=<FILE>;
If you need to read a directory, the command is
@files=<DIR>;
Anything you open you should close: either close(FILE); or closedir(DIR);

While a file is open for writing you can write things to it with the command
print FILE whatever_you_want_ to write;
The command
print whatever_you_want_ to write;
just writes your data to standard output.


Control: Perl supports the same control statements that C does:
if(){}, elsif(){}, and else{}
Note that elsif spelled differently than in C. In C, the {} pair is not required if you only have one statement, but in Perl the brace pair is required even then. The C tests work in Perl as well:
==, <=, >=, !=
but there are lots of others as you gain experience. Perl can test for equality of strings:
$stringA eq $stringB
There are also ne gt ge lt le as well. There are also test for whether or not some string is a file name, a directory name, etc. Perl also has regular expressions which extend even these extensive possibilities.

Loops: Perl supports while-loops and for-loops. The syntax is the same as in C.
There is a version of the C break; statement - in Perl it is the last; statement.
There is a version of the C continue; statement - in Perl it is the next; statement.
There are other loops as well which give Perl a great deal of its power, but we won't pursue them here.

Subroutines: These are the Perl equivalent of C functions.They begin with
sub name_of_subroutine{ code here }
Just like C functions, Perl subroutines can return a value or not. Because Perl has rather different scope rules than C does, there is less need to pass arguments by reference. A variable $xx defined in one subroutine is the same as the variable $xx defined in another UNLESS you make the variable local to the subroutine. This can be done with the statement
my($xx);
Variables are passed to your subroutines on a stack and can be extracted by repeated calls to
shift(@_);

Values can be returned via
return(whatever);


Back.