tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ Piping array contents to a system command

Variable expansion in string ▻

Directory name extraction

In article <4eqs77$8fm@Denied.uprr.com>,
Tom Gerber <twgerber@notes.up.com> wrote:
>I'm running Perl 5 on Windows NT.

Disclaimer: I only grok Unix...

>How do I determine the current directory I'm running my perl script
>from?

    use Cwd;
    $dir = Cwd::getcwd;

>If I'm in
>
>h:\dirA\dirB\dirC
>
>I want to get the name "dirC" into a variable I can manipulate.

I don't think there's a truly portable way to do this, since it
relies on knowing the character[s] your OS uses to separate directory
names components.

    use Cwd;
    $dir = Cwd::getcwd;

    # Assume directory name components are separated either by `/'
    # or `\'.  This should work under Unix (unless you happen to
    # be in a directory with a \ in its name) and NT.
    ($basedir = $dir) =~ s!.*[\\/]!!;

Tim.
--
Tim Goodwin   | "Forget the mystical level numbers." -- Henry Spencer

Original headers:

Path: wave.news.pipex.net!pipex!tim
From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc
Subject: Re: Directory name extraction
Date: 2 Feb 1996 13:00:38 GMT
Organization: Unipalm PIPEX
Message-ID: <4et1tm$ksn@wave.news.pipex.net>
References: <4eqs77$8fm@Denied.uprr.com>

△ comp.lang.perl △

◅ Piping array contents to a system command

Variable expansion in string ▻