Fundamentals of Perl Scripting

A Perl script is effectively similar to a shell script, such as those we saw in Chapter 13. The first line, the “interpreter line,” tells the shell which interpreter to use to run the rest of the script's contents:

#/usr/bin/perl

The rest of the script, as in shell programming, is made up of variable assignments, flow-control blocks and loops, system calls, and operations on I/O handles—to name a few fundamental parts of a script's anatomy. Here's a simple example:

#!/usr/bin/perl

$string = "Hello world!";
$hostname = `hostname`;
if ($hostname eq "uncia") {
  print $string."\n";
  print `date`;
}

Notice the use of C-style curly brackets to delimit the if block, rather than the “if/fi” and “case/esac” syntax of shell ...

Get FreeBSD® Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.