Here’s a quick fix when working with command line parameters in Perl that will take paths, absolute or relative, as an argument.
For example you execute your script like so:
$./foo.pl -path ./my/path
$./foo.pl -path ../../some/path
$./foo.pl -path /absolute/path
All of which could be possible paths entered by users, you want to support all of them and convert them all to absolute paths in your script. This ensures you script can be called from anywhere, and accept any path given.
– begin scrip –
#!/usr/bin/perl
use Cwd ‘abs_path’;
my $root = $ARGV[1];
$root = abs_path($root);
print “$root\n”;
– end –
Should always print an absolute path, ie- starting with the full path “/some/path”.
Anonymous
March 14, 2005 at 3:11 pm
Handy. Thanks for the tip. Scott
redwarp
June 3, 2010 at 8:24 am
Pretty cool, thanks