Presenting Mathematical Concepts on the World Wide Web
Forms & Scripts

Perl: System Calls

One of Perl's strengths is its ability to interact with the underlying operating system. This makes Perl a very effective systems management tool. It is also convenient for CGI scripts.

Directory Operations

Reading directories is similar to reading from files.

foreach $file (readdir(DIR)) {
   print "$file \n";

File Operations

Most common file operations can be performed within Perl. The syntax has its roots in unix, but most basic operations have equivalents in other operating systems.
chdir "/u/rminer/tmp";
mkdir("test", 0755);
. # write stuff to gunk
foreach $file (readdir(DIR)) {
  print "deleted $file \n";

File Tests

There are a number of special file tests.

(-e "dog") || print "no dog exists \n";
(-r "cat") || print "can't write to cat \n";
(-d "rat") || print "rat is NOT a directory \n";

System Calls

Frequently, one wants to run another program from within a Perl script. For example, we may want to run the convert program, or start Maple. This is done with the system command, which passes a string along to the underlying system.
system("ps | grep rminer");

$file1 = "";
$file2 = "test.gif";

system("convert $file1 $file2");

Next: Perl Peculiarities
Back: File I / O
Up: Forms & Scripts

Presenting Mathematical Concepts on the World Wide Web. Copyright © 1997 by Carol Scheftic. All rights reserved. (This section was originally copyrighted in 1996 by The Geometry Center and is re-used here with permission.) Please send comments on this page, or requests for permission to re-use material from this page, to:
Page established 1-Jun-97; last updated Thursday, 24-Jul-1997 01:29:02 CDT.