Figure 2: perl version of example in Figure 1
#!/opt/bin/perl5
#
# Example 2 - perl version of example 1
#
# John Lees lees@cps.msu.edu
# Pattern Recognition & Image Processing Laboratory
# M i c h i g a n S t a t e U n i v e r s i t y
$home = $ENV{'HOME'};
$ls_file = "/tmp/ls.$$";
$cull_file = "/tmp/cull.$$";
$rm_file = "/tmp/rm.$$";
# Patterns for files we may want to remove.
# Standard regular expression syntax applies.
$patterns =
'\\.aux$ \\.bak$ \\.bbl$ \\.blg$ \\.dlg$ \\.dvi$ '
. '\\.glo$ \\.idx$ \\.ind$ \\.lof$ \\.log$ \\.lot$ '
. '\\.old$ \\.ps$ \\.toc$ ^# #$ %$ ~$ ^core$ ^a.out$';
# $SIG is an associative array that holds the name of the
# handler for each signal.
$SIG{'HUP'} = 'sig_all'; $SIG{'INT'} = 'sig_all';
$SIG{'QUIT'} = 'sig_all'; $SIG{'TERM'} = 'sig_all';
if (@ARGV == 0) {
# Create the listing file using "ls -lR".
chdir "$home";
chop($pwd = `pwd`);
print "Building the ls file. Patience...\n";
system("/bin/ls -ablLR $pwd > $ls_file") && do {
print "ls -lR did not complete.\n";
&sig_all;
};
# Look for files that can be removed.
print "Writing the cull file.\n";
open(LS_FILE, "$ls_file") || do {
print "Could not open $ls_file for input.\n";
&handler;
};
open(CULL_FILE, ">$cull_file") || do {
print "Could not open $cull_file for output.\n";
&handler;
};
$dir = "$home";
while (<LS_FILE>) {
# The line just read is in the variable $_.
# Directory.
/:$/ && do {
chop; chop;
$dir = $_;
next;
};
# Skip blank lines and "total nnn" lines.
if (length == 1 || /^total [0-9]*$/) {
next;
}
# Get just the filename.
/\s(\S*)$/;
$file = $1;
# Is this file a candidate for removal?
foreach $p (split(/ /, $patterns)) {
if ($file =~ /$p/) {
if ("$prev" ne "$dir") {
print CULL_FILE "$dir:\n";
$prev = $dir;
}
print CULL_FILE "$_";
} }
} # while
} # No cull file supplied.
else {
# Cull file supplied.
$SIG{'HUP'} = 'sig_ls_rm'; $SIG{'INT'} = 'sig_ls_rm';
$SIG{'QUIT'} = 'sig_ls_rm'; $SIG{'TERM'} = 'sig_ls_rm';
$cull_file = $ARGV[0];
}
# At this point we have a cull file. Ask the user how to
# proceed from here.
MENU: while (1) {
print " 1 Quit, leaving the cull file $cull_file.\n";
print " 2 Create a script file you can run to do the";
print " removals later.\n";
print " 3 Interactively remove the files now.\n";
print " 4 List the cull file; return to this menu.\n";
print "(Ctrl-C to quit, doing nothing.)\n";
print "Enter number of choice (1-4): ";
$choice = <STDIN>;
$choice == '1' && do {
print "The file listing possible files to ";
print "remove is \"$cull_file\".\n";
print "You can rerun $0 ";
print "using that file as the argument.\n";
unlink $ls_file;
exit;
};
$choice == '2' && do {
last MENU;
};
$choice == '3' && do {
last MENU;
};
$choice == '4' && do {
system("/opt/bin/less $cull_file");
next MENU;
};
# Default.
next MENU;
}; # while
# Create a script file.
$choice == '2' && do {
$dir = $ENV{'HOME'};
open(CULL, "$cull_file")
|| die "Could not open $cull_file!\n";
open(RM, ">$rm_file")
|| die "Could not open $rm_file!\n";
while (<CULL>) {
# Directory name.
/:$/ && do {
chop; chop;
$dir = $_;
next;
};
# File name.
/^-/ && do {
/\s(\S*)$/;
print RM "/bin/rm -f $dir/$1\n";
next;
};
}
print "The file containing rm commands is $rm_file\n";
exit;
}; # create script file
# Interactive file removal.
$SIG{'INT'} = 'sig_c';
$choice == '3' && do {
# Read the cull file.
$dir = $ENV{'HOME'};
open(CULL, "$cull_file")
|| die "Could not open $cull_file!\n";
while (<CULL>) {
# Directory name.
/:$/ && do {
chop; chop;
$dir = $_;
next;
};
# File name.
/^-/ && do {
/\s(\S*)$/;
$file = $1;
print "Remove $dir/$file? ";
$_ = <STDIN>;
/^[yY]/ && do {
unlink "$dir/$file";
};
next;
};
}
if (@ARGV == 0) {
# Remove the tmp cull file.
unlink $cull_file;
}
unlink $ls_file;
exit;
}; # interactive removal
# Logical end of main program.
# Signal handler. Remove ALL tmp files.
sub sig_all {
unlink $ls_file;
unlink $cull_file;
unlink $rm_file;
exit 1;
}
# Signal handler. Remove ls and rm tmp files.
sub sig_ls_rm {
unlink $ls_file;
unlink $rm_file;
exit 1;
}
# The Control-C handler.
sub sig_c {
print "Do you want to quit? ";
$_ = <STDIN>;
/^[yY]/ && exit 1;
# Reset the interrupt handler.
$SIG{'INT'} = 'sig_c';
}
|