H
Henry Law
Running under Activestate on Windows XP I have need to feed a series of
directory and file specifications to a program, which then interprets
them. Specifications can be directories, single file names, or wildcard
combinations like c:\foo\bar\*.pdf which result in lists of matching files.
The specifications need to be validated to weed out those that don't
refer to an existing directory, or that result in no matching files.
I've Googled and searched CPAN for a module that does this, since it
must be a common requirement, but without success. Can someone suggest one?
Here's the subroutine I wrote to do the job, so you'll see what I'm
trying to do. If it needs to exist then comments on how to smarten it
up would be welcome.
#! /usr/bin.perl
use strict;
use warnings;
use constant VALID_DIRECTORY => -1;
use constant VALID_FILE => -2;
print "Enter spec: ";
my $spec = <STDIN>;
chomp $spec;
while ($spec) {
my $ret = interpret_spec($spec);
if ($ret == VALID_DIRECTORY) {
print "'$spec' is a valid directory\n";
} elsif ($ret == VALID_FILE) {
print "'$spec' is an existing file\n";
} elsif ($ret) {
print "'$spec' results in $ret files\n";
} else {
print "'$spec' doesn't match any file or directory\n";
}
print "=> ";
$spec=<STDIN>;
chomp $spec;
}
sub interpret_spec{
# Checks a file/directory specification. Returns
# 0 Spec matches nothing
# -1 It's a valid directory specification
# -2 It's a valid single file specification
# number It's a glob, resulting in "number" files
my $spec = shift;
return VALID_DIRECTORY if (-d $spec);
return VALID_FILE if (-f $spec);
my @globs = glob $spec;
if (scalar @globs == 1){
return ((-f $globs[0]) ? 1 : 0);
} else {
return scalar @globs;
}
}
directory and file specifications to a program, which then interprets
them. Specifications can be directories, single file names, or wildcard
combinations like c:\foo\bar\*.pdf which result in lists of matching files.
The specifications need to be validated to weed out those that don't
refer to an existing directory, or that result in no matching files.
I've Googled and searched CPAN for a module that does this, since it
must be a common requirement, but without success. Can someone suggest one?
Here's the subroutine I wrote to do the job, so you'll see what I'm
trying to do. If it needs to exist then comments on how to smarten it
up would be welcome.
#! /usr/bin.perl
use strict;
use warnings;
use constant VALID_DIRECTORY => -1;
use constant VALID_FILE => -2;
print "Enter spec: ";
my $spec = <STDIN>;
chomp $spec;
while ($spec) {
my $ret = interpret_spec($spec);
if ($ret == VALID_DIRECTORY) {
print "'$spec' is a valid directory\n";
} elsif ($ret == VALID_FILE) {
print "'$spec' is an existing file\n";
} elsif ($ret) {
print "'$spec' results in $ret files\n";
} else {
print "'$spec' doesn't match any file or directory\n";
}
print "=> ";
$spec=<STDIN>;
chomp $spec;
}
sub interpret_spec{
# Checks a file/directory specification. Returns
# 0 Spec matches nothing
# -1 It's a valid directory specification
# -2 It's a valid single file specification
# number It's a glob, resulting in "number" files
my $spec = shift;
return VALID_DIRECTORY if (-d $spec);
return VALID_FILE if (-f $spec);
my @globs = glob $spec;
if (scalar @globs == 1){
return ((-f $globs[0]) ? 1 : 0);
} else {
return scalar @globs;
}
}