return to first page linux journal archive
keywordscontents

Listing 3. Perl Program to Remove and Create Files

#!/usr/bin/perl -wT
# This program, meant to be run daily via a cron
# job, removes any existing file (or symbolic link)
# with the name today.html, and then creates a new
# symlink from today.html to file-n.html, 
# where 
use strict;
use diagnostics;
use vars qw(@now $day $file $today $successful);
# Get the current time and date
@now = localtime(time);
# Get the day of the week
$day = $now[6];
# Set $file to today's file
$file = "/file-$day.html";
# Set $today to the name of the symlink
$today = "/today.html";
# Remove the existing file, if it exists
if (-e $today)
{
    # Remove the file
    $successful = unlink($today);
    # Indicate whether the file was deleted
    if ($successful)
    {
	print "Successfully deleted \"$today\"\n";
    }
    else
    {
	print "Problem deleting \"$today\": $! \n";
    }
}
# 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\"\n";
}
else
{
    print "Problem creating symlink \"$today\" ->
\"$file\": $! \n";
}