Converting paths to absolute in Perl

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”.

VN:F [1.8.4_1055]
Rating: 9.0/10 (1 vote cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)
Converting paths to absolute in Perl9.0101

One Response to “Converting paths to absolute in Perl”

  1. Anonymous says:

    Handy. Thanks for the tip. Scott

    UN:F [1.8.4_1055]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.4_1055]
    Rating: 0 (from 0 votes)

Leave a Reply