use File::Basename;
my $file = basename($full_path);
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply. | [reply] [d/l] |
Just for completeness, the way I've been doing it (because I didn't know about File::Basename either) is this:
my ( $basename ) = reverse split /\//, "/tmp/app/foo";
Now I'm going to go back and change my code to use File::Basename. Thanks, merlyn!
-- Spring: Forces, Coiled Again!
| [reply] [d/l] |
> my ( $basename ) = reverse split /\//, "/tmp/app/foo";
The only problem with this is that it's not portable: it'll work on Unix, but probably not on MS Windows.
| [reply] |
There are two simple ways of answering this question. One is to give you what you asked for: a regex to get the filename component from a UNIX-style path. That regex is m|(.[^/]+)$/ (Read: a bunch of non-slash characters (the maximum number possible, more or less), then the end of the string (with various caveats on exactly what the end of string means).
The other is to inform you of the right way to do it, which is to use File::Basename, which will do the right thing regardless of platform, and also makes the intent of your code more clear.
Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).
| [reply] [d/l] |
theorbtwo wrote: > That regex is m|(.[^/]+)$/
Thanks for that answer, which I think is correct in several respects (including the way it provides a direct answer to the question as originally posed alongside the statement of fact that there might be an easier way to do the job).
The one thing I would point out is that maybe you meant to say:
m|(.[^/]+)$| -or-
/(.[^/]+)$/
...All the world looks like -well- all the world,
when your hammer is Perl. ---v
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
| [reply] |
$ perl -MFile::Basename -e'$f="/usr/local/bin/foo"; print basename( $
+f );'
foo
| [reply] [d/l] |