how to combine directory path in perl



I am having a perl script in which i am giving path to directory as input. Directory has xml files inside it.


In my code i am iterating through all the xml files and creating absolute path for all xml files. Code is working fine.



#!/usr/bin/perl

use File::Spec;

$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}

my $dirPath = $ARGV[0];

opendir(DIR, $dirPath);
my @docs = grep(/\.xml$/,readdir(DIR));

foreach my $file (@docs)
{
my $abs_path = join("",$dir,$file);

print "absolute path is $abs_path";
}


Question i have here is, joining $dirPath and $file with no separator which means that $dirPath must end in a "/". So is there any way or built in function in perl which take cares of this condition and replaces the join method.


All i want is not to worry about the separator "/". Even if script is called with path as "/test/dir_to_process" or "/test/dir_to_process/", i should be able to produce the correct absolute path to all xml files present without worrying about the separator.


Let me know if anyone has any suggestions.


No comments:

Post a Comment