#!/usr/bin/perl -w
# @ Author: Loren Segal
# @ Date: January 2005
#
# AED: Academic Editor
#######################
# This program can be used to print or modify file contents
#
# On a side note, while it would be a lot quicker to simply
# assume this script will run solely on a UNIX/Linux machine,
# it's not correct to do so, when there are only minor adjustments
# needed to allow this program to function in windows. This
# note is specific for the list command, where on a UNIX machine
# one could simply do things like:
#
# `ls -l | grep -e $regex`
#
# However, since this is a practice in file I/O, it's probably best
# to use the proper perl functions.
#
# AED Usage notes:
# -----------------
# Command Listing:
# list [regexp] #- Lists a directory matching regex.
# disp <infile> [regexp] [outfile] #- Displays a file to stdout or a file
# # matching optional regex.
# edit <infile> [reg-match] #- Replaces text matching reg-match with
# [reg-replace] [outfile] # reg-replace in infile and prints to
# # outfile or stdout.
# help #- Prints this help.
# exit #- Exits aed.
#
use strict; # since the point is to learn perl (properly),
# I may as well impose the strict syntax.
# Prototypes
sub main();
sub usage();
sub ls($);
sub disp(@);
sub edit(@);
# END PROTOTYPES
# Begin program listing
main();
# END PROGRAM
sub main()
{
while (1)
{
print "aed: ";
chomp($_ = <STDIN>); # Get user input
my($cmd, @args) = split;
next if (!$cmd);
if ($cmd eq "help") { usage(); } # help
elsif ($cmd eq "list") { ls($args[0]); } # list [regexp]
elsif ($cmd eq "disp") { disp(@args); } # disp <infile> ...
elsif ($cmd eq "edit") { edit(@args); } # edit <infile> ...
elsif ($cmd eq "exit") { last; } # exit
else { print `$_`; } # (other) / Send to shell
}
}
sub usage()
{
print
"
Command Listing:
list [regexp] #- Lists a directory matching regex.
disp <infile> [regexp] [outfile] #- Displays a file to stdout or a file
# matching optional regex.
edit <infile> [reg-match] #- Replaces text matching reg-match with
[reg-replace] [outfile] # reg-replace in infile and prints to
# outfile or stdout.
help #- Prints this help.
exit #- Exits aed.
";
}
sub ls($)
{
my($re) = @_;
opendir(DIR, ".") || print STDERR "Cannot open directory: .";
print "\nListing directory: " . (`pwd` || '.') . "\n\n";
for (readdir(DIR)) {
print "$_\n" if (($re && /$re/) || !$re);
}
print "\n\nEnd of directory listing.\n\n";
closedir DIR;
}
sub disp(@)
{
my($infile, $re, $outfile) = @_;
my(@file);
if (!$infile) {
print STDERR "Usage: disp <infile> [regexp] [outfile]\n";
return;
}
if (!open(IN, "< $infile")) {
print STDERR "No such file: $infile\n";
return;
}
if ($outfile) {
open(OUT, "> $outfile");
}
@file = <IN>;
if ($re) { @file = grep(/$re/, @file); }
if ($outfile) { print OUT @file; close(OUT); }
else { print @file; }
close(IN);
}
sub edit(@)
{
my($infile, $rematch, $rereplace, $outfile) = @_;
my(@file);
if (!$infile) {
print STDERR "Usage: disp <infile> [regexp] [outfile]\n";
return;
}
if (!open(IN, "< $infile")) {
print STDERR "No such file: $infile\n";
return;
}
if ($outfile) {
open(OUT, "> $outfile");
}
@file = <IN>;
if ($rematch) { grep(s/$rematch/$rereplace/, @file); }
if ($outfile) { print OUT @file; close(OUT); }
else { print @file; }
close(IN);
} Powered by
GeSHi Syntax Highlighting software.
Author of all (other) material unless otherwise specified:
Loren Segal. Copyright 2005.