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
2010-04-02 01:50:01
just a filename without any leading or trailing slashes
Zerobu
2010-04-02 01:54:43
what do you mean by "just a filename"?
ysth
2010-04-02 01:58:39
@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
2010-04-02 01:59:04
like the name of the file with the extensionExample hello.txt or hello.gif,
Zerobu
2010-04-02 01:59:53
Yes, show some examples and I can give you a better answer.
brian d foy
2010-04-02 02:14:10
If you just have the filename and nothing else, you're out of luck.
brian d foy
2010-04-02 02:16:06
Also, please update your question with additional information. Don't bury it in comments.
brian d foy
2010-04-02 02:16:35
A:
You can use the most brilliant brian's method above and if the $directory
eq "." then replace "." with your currrent working directory.
hpavc
2010-04-02 02:49:10