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.
opendir(DIR,"/u/rminer/tmp");

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

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);
rename("test","gunk");
.
. # write stuff to gunk
.
opendir(DIR,"gunk");
foreach $file (readdir(DIR)) {
  unlink("gunk/$file");
  print "deleted $file \n";
}
closedir(DIR);

File Tests

There are a number of special file tests.
chdir("/u/rminer/tmp");

(-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 = "test.ps";
$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: scheftic@geom.umn.edu
Page established 1-Jun-97; last updated Thursday, 24-Jul-1997 01:29:02 CDT.