Some features of Perl


$foo - indicates a normal variable:

	$foo = 123;

	$foo += 99;

	print "Current value = $foo\n";

@bar - indicates an array:

	@bar = (1..9);

	push(@bar, 10);

	print "9th element of the array is $bar[8]\n";

%plugh - indicates an associate (or keyed) array:

	%plugh = ('act', 'Australian Capital Territory', 
		  'nsw', 'New South Wales', 
		  'qld', 'Queensland',
		  'vic', 'Victoria',
		  'tas', 'Tasmania');

	$state = 'vic';
	print "The eastern state of choice is $plugh{$state}.\n";

	foreach $entry (sort keys(%plugh)) {
	  print "$entry = $plugh{$entry}\n";

File handling

Perl is very capable when it comes to handling files. For example:

	open(FILE, "| $temp");

	open(OUT, ">> $output");

	chown, chmod, readdir, rename, unlink, etc.

	dbmopen(%DATA, '/usr/lib/news/history', 0666);

Interfacing to other programs

Perl can interface into other programs in a number of ways. For example:

	system "ln -s $new $old";

	@data = `finger`;

	open(MAIL, "| /bin/mail $user") || die "Can't open mail!\n";
	print MAIL @results;

Pattern matching & substitution

Perl's pattern matching capabilities are based on those in the Version 8 regexp routines. Some examples of the power of Perl's pattern matching include:

	$xyzzy =~ s/\bapple/banana/g;	# replace apple but not grapple

	$foo = "Data for $1 = $2" if /^User: (\w+),\s*Output: (\d+)$/;


Perl supports the C-style printf command as well as print. In addition to these output commands is the write command which uses format statements to print nicely formatted reports.

Reports can have headers printed at the top of each page, and output can be left aligned (@<), centered (@|) or right aligned (@>).

For example:

#!/usr/local/bin/perl -- -*- perl -*-

while (($user, $pwd, $uid, $gid) = getpwent) {

format STDOUT_TOP =

     User    UID    GID

format STDOUT =
  @>>>>>>>>  @<<<<< @<<<<<
  $user,     $uid,  $gid
would generate a report that looked something like this:

     User    UID    GID
       root  0      0
        bin  1      1
     daemon  2      2
        adm  3      4
         lp  4      7
       sync  5      0
   shutdown  6      0


A simple program that illustrates how to use sub-routines in Perl:

#!/usr/local/bin/perl -- -*- perl -*-
# a simple /etc/passwd checker

while (($user, $pwd, $uid, $gid, $q, $gcos, $comm, $dir, $shell) = getpwent) {

  &Alert("No password for $user") if $pwd eq '';
  &Alert("$dir directory doesn't exist for $user") if !(-d $dir);

  # plenty of room for additional checking (duplicate UIDs, etc)
print "Total errors: $errors\n";

sub Alert {
  local($message) = pop(@_);

  print "WARNING: $message\n";

Inline commands

Perl works as easily from the command line as it does from a script. For example:

To change a word (ignoring case) and backup the original file(s):

	perl -p -i.bak -e 's/graham/Graeme/ig' FILES

A faster way of deleting all files older than a fortnight in /tmp:

	find /tmp -mtime +14 -print | perl -nle 'unlink;'

Next - Perl & Linux Updated: 5 December, 1995