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.
Before
opendir(DIR, $dir) or die "cannot open directory";
check if directory exists and if not assign$dir
variable default value:The first and most critical mistake you have made is to not use
These two pragmas provide more help than any other two lines of code you could possibly add. Never write Perl code without them.
You do not use either of these modules, so for this code, they are redundant.
This can be written
Because an array in scalar context returns its size.
Unless you really need the precision of
readdir
, what you most often want isglob()
. This code will replace the above code: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
.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:The
constant
pragma and a conditional statement modifier would make your default logic rather readable:(And, yes, do take notice of others' suggestions: strict, warnings, lexical dirhandles, pruning unneeded modules, avoiding
$#array
, etc.)