[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Re: extracting last part of cwd
2000-01-12-12:38:26 lewst:
> I apologize for the "newbie" nature of this question, but
> dejanews has been down for 2 days so I can't post to the
> newsgroup or search for answers.
This inspires in me a couple of questions for the wizards on the
perl5-porters mailing list:
Is there a "perl-users" mailing list out there anywhere?
If not, maybe there should be, for the benefit of people who not
have access to netnews? It's easy enough to set up a list with
automatic archiving these days; would it be a good idea?
> I'm need to extract only the last element of the current working
> directory and store it in a variable but can't figure out how.
Using Cwd::cwd to get the path is a fine start.
Once you have it in $mycwd, there are many ways to proceed. A
couple of them come to mind immediately in the category of basic
string processing. If you are working on a system that uses "/" to
separate the directories in a pathname, then a couple of ways to
pull out the dir would be
my $dir = (split '/', $mycwd)[-1];
my($dir) = ($mycwd =~ m#/([^/]+)$#);
If you are more interested in portability, I'd tend to recommend
File::Basename:
use File::Basename;
my $dir = basename $mycwd;
That should so something appropriate regardless of how your local OS
writes pathnames.
-Bennett
PGP signature
- References to:
-
lewst <lewst@yahoo.com>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]