Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a path in perl with full filename?

Tags:

path

split

perl

I want to split a path can anyone help me?

My input is:

$getpath = "/u/project/path/file.name";

I want to split /u/project/path in one variable and file.name in other variable.

like image 438
Mak_Thareja Avatar asked Nov 30 '22 04:11

Mak_Thareja


2 Answers

File::Basename can help you extract the informations you need (and is part of the core modules)

  my($filename, $directories, $suffix) = fileparse($path);
like image 175
ccheneson Avatar answered Dec 04 '22 15:12

ccheneson


First of all, there are many perl modules can do what you want, try searching on the CPAN. Second, I suggest using File::Spec module. For example:

use File::Spec;
($volume,$directories,$file) = File::Spec->splitpath( $path );

then $directories will be "/u/project/path", and $file will be "file.name".

File::Spec module is capable of five operating systems: Unix(Linux), Mac, Win32, OS2, VMS. And this module also offers tons of other file operations like catpath, updir, file_name_is_absolute, etc. You don't need to change your codes on different systems.

Ref: File::Spec

like image 25
noalac Avatar answered Dec 04 '22 15:12

noalac