Next: Perl Peculiarities
Up: CGI Scripts with Perl
Prev: File I/O

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
Up: CGI Scripts with Perl
Prev: File I/O

[HOME] The Geometry Center Home Page

Comments to: webmaster@www.geom.uiuc.edu
Created: May 29 1996 --- Last modified: May 29 1996