LJ Archive

Listing 5. Creating a Symbolic Link

#!/usr/bin/perl -wT
# This program creates a symlink from today.html
# to file-n.html, where n is the highest
# numbered file in the directory.
use strict;
use vars qw($directory @files $file $destination
    $successful);
use diagnostics;
# Set the appropriate directory
$directory = "/home/reuven/";
if (opendir(DIR, $directory))
{
    @files = grep {/^file-[0-9]+\.html$/}
         readdir(DIR);
    closedir DIR;
}
else
{
    &log_and_die("Problem opening $directory;" .
     "today's file unavailable.");
}
# Determine today's file
$file = $files[$#files];
$destination = "/today.html";
# Create a symbolic link to today's file
$successful = symlink($file, $today);
# Indicate if there was a problem
if ($successful)
{
    print "Successfully created link $today -> $file";
}
else
{
    print "Problem creating symlink"
       unless $successful;
}
LJ Archive