[comp.lang.perl] pwd?

thoth@reef.cis.ufl.edu (Robert Forsman) (04/17/91)

  Oh, and while you're at it:

  I have a path in $_ that is relative to the current working
directory.  What is the absolute path to that file?

  Hint: it's NOT `pwd`.$_
--
"Rob, you're stupid, and that makes you dangerous." - chess opponent
I deal with Reality as you _don't_ understand it. 
"The way I see things, food exists to give texture to the taste of ketchup."

mgemmel@cs.vu.nl (Martin Gemmel) (04/17/91)

In article <irrelevant> thoth@reef.cis.ufl.edu (Robert Forsman) wrote:
>  I have a path in $_ that is relative to the current working
>directory.  What is the absolute path to that file?
>
>  Hint: it's NOT `pwd`.$_

No, but how about: $ENV{'PWD'}.'/'.$_

>--
>"Rob, you're stupid, and that makes you dangerous." ...
>[...]
8-)

Martin

me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) (04/17/91)

Here's what I use to get an absolute path. As always, comments welcomed.

Wayne

sub FullPath {  # FullPath ($name)
    local ($name, $path, $dir) = @_;
    if ($name =~ m#^/#) {
        $path = $name;
    }
    elsif ($name !~ m#/#) {
        foreach $dir (split (/:/, $ENV{'PATH'})) {
            $path = join ('/', $dir, $name);
            last if -x $path && -f _;
            $path = '';
        }
    }
    else {
        chop ($dir = `pwd`), $path = join ('/', $dir, $name);
    }
    chop ($dir = `pwd`), $path = join ('/', $dir, $name)
        if $path eq "./$name";
    $path =~ s#[^/]+/\.\.##go;
    $path =~ s#\./##go;
    $path =~ s#//#/#go;
    $path;
}
1;

merlyn@iwarp.intel.com (Randal L. Schwartz) (04/17/91)

In article <6330@male.EBay.Sun.COM>, me@anywhere (Wayne Thompson - IR Workstation Support SE) writes:
| Here's what I use to get an absolute path. As always, comments welcomed.
| 
| Wayne
| 
| sub FullPath {  # FullPath ($name)
|     local ($name, $path, $dir) = @_;
|     if ($name =~ m#^/#) {
|         $path = $name;
|     }
|     elsif ($name !~ m#/#) {
|         foreach $dir (split (/:/, $ENV{'PATH'})) {
|             $path = join ('/', $dir, $name);
|             last if -x $path && -f _;
|             $path = '';
|         }
|     }
|     else {
|         chop ($dir = `pwd`), $path = join ('/', $dir, $name);
|     }
|     chop ($dir = `pwd`), $path = join ('/', $dir, $name)
|         if $path eq "./$name";
|     $path =~ s#[^/]+/\.\.##go;
|     $path =~ s#\./##go;
|     $path =~ s#//#/#go;
|     $path;
| }
| 1;

Which doesn't work if $name contains "foo/../bar", where "foo" is a
symlink.  Unless you're willing to stat a lot of directories, the fastest
way is:

	chop($path = `cd $name && pwd`);

$z="/tmp/Just another Perl hacker,";mkdir($z,0777);chdir$z;$_=`pwd`;chdir;rmdir$z;s#.*/##;print
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

rbj@uunet.UU.NET (Root Boy Jim) (04/19/91)

merlyn@iwarp.intel.com (Randal L. Schwartz) writes:
>Unless you're willing to stat a lot of directories, the fastest way is:
>
>	chop($path = `cd $name && pwd`);

I have never seen a shell that treated cd properly with respect
to && and ||. Try "cd /bogon || echo failed".

However, the example you give does leave $path with nothing,
so perhaps it "works" after all.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

rbj@uunet.UU.NET (Root Boy Jim) (04/23/91)

In <T.7_.8_@smurf.sub.org> urlichs@smurf.sub.org (Matthias Urlichs) writes:
?In comp.lang.perl, article <129476@uunet.UU.NET>,
?  rbj@uunet.UU.NET (Root Boy Jim) writes:
?< 
?< I have never seen a shell that treated cd properly with respect
?< to && and ||. Try "cd /bogon || echo failed".
?
?Perhaps you should look at bash. Or the latest ksh.

Which version of ksh?

And I may never use bash. I'd like to, but it just doesn't work.
And on a Sparc running 4.1.1, no less. "history" core dumps.
Things are no better than they were years ago.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

les@chinet.chi.il.us (Leslie Mikesell) (04/23/91)

In article <129476@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
>>Unless you're willing to stat a lot of directories, the fastest way is:
>>
>>	chop($path = `cd $name && pwd`);

>I have never seen a shell that treated cd properly with respect
>to && and ||. Try "cd /bogon || echo failed".

The "proper" thing for any shell that offers to run /bin/sh scripts
to do is to exit immediately with an error status when a cd fails.
Unfortunatly some don't, and more unfortunate there is no way to
control it (i.e. IMHO as a sometimes useful extension there should
be an option to enable it in a script that expects to continue past
a cd).  As it is, all those historically correct shell scripts that
cd somewhere ; rm -f * will happily eat your current directory when
you replace /bin/sh with ksh and "somewhere" goes away.
Anyway, just to maintain some relevance to perl - in the above case
it doesn't matter much.

Les Mikesell
  les@chinet.chi.il.us

rbj@uunet.UU.NET (Root Boy Jim) (04/25/91)

In article <1991Apr23.145850.6064@chinet.chi.il.us> les@chinet.chi.il.us (Leslie Mikesell) writes:
?In article <129476@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
?
?>I have never seen a shell that treated cd properly with respect
?>to && and ||. Try "cd /bogon || echo failed".

What am I talking about? Both my ksh's do the right thing.

?The "proper" thing for any shell that offers to run /bin/sh scripts
?to do is to exit immediately with an error status when a cd fails.

Yeah, but I'd much rather see 'cd' return a status just like a command.
Since you quoted "proper", perhaps you do as well?

?Unfortunatly some don't, and more unfortunate there is no way to
?control it (i.e. IMHO as a sometimes useful extension there should
?be an option to enable it in a script that expects to continue past
?a cd).

That's what the -e option is for.

?As it is, all those historically correct shell scripts that
?cd somewhere ; rm -f * will happily eat your current directory when
?you replace /bin/sh with ksh and "somewhere" goes away.

Sometimes fixing bugs is dangerous.

?Anyway, just to maintain some relevance to perl - in the above case
?it doesn't matter much.
?
?Les Mikesell
?  les@chinet.chi.il.us


-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane