2011-11-15 17 views
12

Tengo la ruta completa a un archivo y la ruta completa a uno de sus directorios principales en dos variables en el programa Perl.¿Cómo convierto una ruta completa a una ruta relativa usando perl?

¿Cuál es una forma segura de calcular la ruta relativa del archivo en relación con el directorio principal. Necesita trabajar en Windows y Unix.

p. Ej.

$filePath = "/full/path/to/my/file"; 
$parentPath = "/full"; 
$relativePath = ??? # should be "path/to/my/file" 
+0

relacionados http://stackoverflow.com/preguntas/6278143/how-to-assign-result-of-a-regex-match-to-a-new-variable-in-a-single-line – daxim

Respuesta

22

Uso File::Spec

Tienen una función abs2rel

my $relativePath = File::Spec->abs2rel ($filePath, $parentPath); 

funcionará en Windows y Linux

9
use Path::Class; 
my $full = file("/full/path/to/my/file"); 
my $relative = $full->relative("/full"); 
+3

+1 Me gusta [Path :: Class] (http: // p3rl. org/Path :: Class), documenta algunas verrugas de diseño en File :: Spec. – daxim

Cuestiones relacionadas