I am having a perl script which is taking a directory path as input and then script iterates through all the files present in that directory. For each file it calls some other function.
Here is the script:
#!/usr/bin/perl
use JSON;
use XML::Simple;
use File::Spec;
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
my $dir = $ARGV[0];
opendir(DIR, $dir) or die "cannot open directory";
@docs = grep(/\.xml$/,readdir(DIR));
foreach $file (@docs)
{
my $abs_path = join("",$dir,$file);
#print $abs_path;
my $return_object = some_function_call($abs_path);
print_output($return_object);
}
sub print_output
{
my $output_object = $_[0];
print $output_object;
}
My question here is when i am reading input directory, in case if does not exists how can make above script read from "Some Default Directory location". I tried adding some logic but did not work for me. This is my first perl script so please excuse me if this is too basic to ask.
I am working in LINUX environment.
The first and most critical mistake you have made is to not use
use strict;
use warnings;
These two pragmas provide more help than any other two lines of code you could possibly add. Never write Perl code without them.
use JSON;
use XML::Simple;
use File::Spec;
You do not use either of these modules, so for this code, they are redundant.
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
This can be written
if (@ARGV != 1) {
print ...
exit;
}
Because an array in scalar context returns its size.
opendir(DIR, $dir) or die "cannot open directory";
@docs = grep(/\.xml$/,readdir(DIR));
foreach $file (@docs) {
my $abs_path = join("",$dir,$file);
Unless you really need the precision of readdir
, what you most often want is glob()
. This code will replace the above code:
my @docs = glob "$dir/*.xml";
It will return the relative path (not the absolute path, as your variable name implies -- unless the path argument is absolute). For example: examples/foo.xml
.
sub print_output {
my $output_object = $_[0];
print $output_object;
}
I assume this sub is just an example, because it does nothing, and can be reduced to a simple print
.
As to your question about default directory to read from, that to me is at odds with the requirement of an argument (where you check @ARGV
). But you can do a simple check for existence:
if (not -e $dir) {
$dir = "default";
}
The constant
pragma and a conditional statement modifier would make your default logic rather readable:
....
use constant DEFAULT_DIR => '/path/to/default/dir';
....
my $dir = $ARGV[0];
$dir = DEFAULT_DIR unless -e $dir;
....
(And, yes, do take notice of others' suggestions: strict, warnings, lexical dirhandles, pruning unneeded modules, avoiding $#array
, etc.)
Before opendir(DIR, $dir) or die "cannot open directory";
check if directory exists and if not assign $dir
variable default value:
$default_dir = "....";
if (!-e $dir)
{
$dir = $default_dir;
}