views:

256

answers:

3

Is there some function that returns the parent directory of a file in Perl?

+2  A: 

If you have a path name, you can use the dirname function from File::Basename.

 use File::Basename;
 my $directory = dirname( $path );

What do you have as input?

brian d foy
just a filename without any leading or trailing slashes
Zerobu
what do you mean by "just a filename"?
ysth
@Zerobu: So it's a relative path. `dirname` will still give you what you want, with the possible exception of files in the current directory - look at the documentation and see for yourself.
Jefromi
like the name of the file with the extensionExample hello.txt or hello.gif,
Zerobu
Yes, show some examples and I can give you a better answer.
brian d foy
If you just have the filename and nothing else, you're out of luck.
brian d foy
Also, please update your question with additional information. Don't bury it in comments.
brian d foy
A: 

You can use the most brilliant brian's method above and if the $directory eq "." then replace "." with your currrent working directory.

hpavc
A: 

Also note Path::Class, which makes working with paths sane.

wolverian