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.
File::Basename can help you extract the informations you need (and is part of the core modules)
my($filename, $directories, $suffix) = fileparse($path);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With