[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] the directory where the script is running



At 17:34 -0400 5/20/99, Ronald J Kimball wrote:
> On Thu, May 20, 1999 at 01:33:32PM -0700, Vicki Brown wrote:
>> He didn't ask "How do I get the current directory".
>>
>> He asked "How do I find the directory where the script is running".
>
> Perhaps he meant to ask "How do I find the directory where the script
> resides?"
>
> The current working directory is the directory where the script is
>running.

As I keep saying, this is true ONLY on a Macintosh. It is almost invariably
false on a Unix machine. I am talking portability issues (have been, all
along).

At 14:39 -0700 5/20/99, Peter Prymmer wrote:
> Indeed, thanks for the clarification.  I thought there was also a need
> to keep track of things during chdir()s - though perhaps I missed
>something
> there too(?).  I think the code that Chris Nandor posted with the use of
> File::Basename and $0 will cover most unices, even those that store only
> basenames in things like $0 under perl.

As I said before... usually. If the script is on the path OR you call it
with an absolute path. But Not with a relative path.  Trust me.  Try this:

    #!/usr/bin/perl

   print "current working directory is ", `pwd`, "\n";

   print "\$0 is ", $0, "\n";


We install this script on the path somewhere (/usr/local/bin, say)

Now, we run it from my home directory, as "foo"

   current working directory is /export/home/vlb

   $0 is /usr/local/bin/foo

So far, so good.

At 15:22 -0700 5/19/99, Vicki Brown wrote:
> If a script starts with #!...perl  AND the script is on Unix AND the
>script
> is on your path or named by an absolute pathname, then $0 will contain the
> full path to the script.

The location of the script is there in $0; use File::Basename (or a regexp)
to get it out.

What if we go to the directory above where the script is and call it by a
relative path? (./bin/foo)

   current working directory is /usr/usr2

   $0 is ./bin/foo

At 15:22 -0700 5/19/99, Vicki Brown wrote:
> If the script is called by a relative pathname, then $0 contains that
> relative path (and you have to figure out the location by where it is in
> terms of the current directory.)

$0 is not enough. File::Basename plus $0 is insufficient. You have to
figure out the location by where it is in terms of the current directory.

Are ya'll with me now?  'Cause I'm going home :-)

- Vicki

-- --
       |\      _,,,---,,_       Vicki Brown <vlb@cfcl.com>
 ZZZzz /,`.-'`'    -.  ;-;;,_   Journeyman Sourceror: Scripts & Philtres
      |,4-  ) )-,_. ,\ (  `'-'  P.O. Box 1269  San Bruno  CA  94066
     '---''(_/--'  `-'\_) http://www.cfcl.com/~vlb  http://www.macperl.org

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org