System Administration in Perl

These notes talk about Perl's system command and Perl's way to retrieve time.

Perl's System Command

When possible you will want to use Perl's built-in commands for manipulating the file system and other state information connected with the operating system. However sometimes you know which platform you are operating on and you really need access to one of that platform's system commands. Perl's system command provides a way to execute a shell command on the underlying platform. For example, if you are running on a Unix platform, the command:

system("ls -l")
will print a long listing of files to stdout.

Here are some things you should know about the system command:

  1. It returns 0 if the command succeeds and 1 if it fails. These return values are the inverse of most commands, which typically return 0 when they fail and 1 when they succeed.

  2. You can capture system's output by putting the command in backquotes. For example:
         @files = `ls -l`;   # puts the files into an array
         $myfiles = `ls -l`; # puts the files into a string, separated by the \n char
    In other words, `cmd` also invokes the system command. Perl interpolates variables between backquotes, just as in ordinary strings. For example:
         # get info about all .perl files in the current directory
         @files_of_interest = <*.perl>;
         @file_info = `ls -l @files_of_interest`; 

  3. Perl creates a child process and then sleeps while the command is executed.

  4. $^O (capital 'O') is a string with the name of the current operating system, such as MSWin32, linux, or darwin. Note that if you are running on a unix platform, then $^O returns the name of the current unix dialect, not "unix".

    You can use $^O to execute a command with similar functionality but different names on different platforms. For example Unix uses ls and Windows uses dir to list the directory contents, so the following code would get a directory listing on either a Windows or Unix machine:

         if ($^O eq 'MSWin32') {
         else { # better hope we're on a Unix platform!
    Of course the better and safer command to use to list the current directory contents would be Perl's built-in glob operator:
         print join "\n", <*> . "\n";
  5. You can access environment variables through the %ENV hash table. Perl inherits its environment variables from its parent process but you are free to modify them before calling system. All child processes, including your system calls, will then access your modified environment variables.

Time Functions

Perl provides two useful functions for accessing the current time:

  1. time returns the current time from the system clock
  2. localtime queries the system clock and returns the time in a parsed and user-friendly format.

    1. When used in a scalar context localtime returns a user friendly string:
      	  $time = localtime;
      	  print $time; # prints 'Tue Feb  5 11:48:53 2008'
    2. When used in an array context it returns a list of items in the following order:

      • seconds: a number between 0 and 59
      • minutes: a number between 0 and 59
      • hour: hours in military time, or equivalently, the number of hours since midnight. 0 will be the minimum number returned and 23 will be the maximum number returned.
      • day: a number between 1 and 31 representing the current day.
      • month: a number between 0 and 11 with 0 being January and 11 being December. Months are numbered this way to make it easy to index into an array of months.
      • year: the number of years since 1900. For example, during 2008 localtime will return 108 as the year.
      • week day: number of days since Sunday, with Sunday starting at 0 and Saturday being 6.
      • year day: number of days since the beginning of the year with 0 being January 1 and 364 being Dec. 31 (365 on a leap year).
      • day light saving time: true (1) if day light saving time is in effect, and false (0) otherwise.