Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

cwd pragma (rfc)

by sh1tn (Priest)
on Jun 27, 2006 at 21:58 UTC ( [id://557879]=perlquestion: print w/replies, xml ) Need Help??

sh1tn has asked for the wisdom of the Perl Monks concerning the following question:

I had to get rid of absolute paths, especially in production environment. I have to be sure, that my perl scripts have current working directory set to the their own directory.
package cwd; use Carp; use File::Basename; use Cwd qw(realpath getcwd chdir); my $current; BEGIN { $current = getcwd; my $basename = basename $0; my $realpath = realpath $0; $realpath =~ s/$basename$//; chdir $realpath or croak "cannot chdir: $!\n"; } END { chdir $current or croak "cannot chdir: $!\n"; } 1

Now I can invoke callee program, which will execute in its own directory:
# /tmp/caller/CALLER # /tmp/callee/CALLEE # CALLEE content: use strict; use cwd; use Cwd; map{ print $_,$/ }glob'*'; print "\ncallee dir:", getcwd, "\n"; # CALLER content: use strict; use Cwd; print qx|/tmp/callee/CALLEE|; print "\ncaller dir:", getcwd, "\n"; # output (from `cd /tmp/caller; ./CALLER`): CALLEE callee dir:/tmp/callee caller dir:/tmp/caller


Replies are listed 'Best First'.
Re: cwd pragma (rfc)
by japhy (Canon) on Jun 27, 2006 at 22:34 UTC
    I would suggest changing $realpath =~ s/$basename$//; to $realpath =~ s/\Q$basename\E$//;

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      or
      substr($realpath, -length($basename)) = '' if substr($realpath, -length($basename)) eq $basename;
        Maybe just
        $realpath = join(q//, split(/.[^\/]+$/,$realpath));


Re: cwd pragma (rfc)
by Tanktalus (Canon) on Jun 28, 2006 at 04:04 UTC

    It's always a good idea for applications to know what their current working directory is, or to make it immaterial.

    In my case, I started out in the reverse scenario. I started with merely using the path where my executable lived as a base, and used File::Spec's catdir and catfile to build the absolute paths to any files I needed. Then I put most of those (at least the directories) into another module where I could then do Locations->config_dir() and Locations->data(). And then use those to build up even further. Why? Because when I log/trace the path to whatever I'm looking for, having an absolute path allows the human who is reading it to better figure out if it's right or not.

    Then, somewhat mimicking what you're doing, I wrote a task hierarchy. For the same set of scripts. And the task manager object would actually save the current working directory before running each task, and reset it after the task, so that the task could do whatever it wanted, and I wouldn't worry about how adding a new task may make an old task stop working. At least by changing the cwd. Mostly, I only use this when building tarballs automatically. Since I no longer have to worry about the current directory (or resetting it), I don't need to write my fallback: fork, chdir, exec.

    However, at no time should you need to worry about the current directory in END. Since the current directory is merely part of your process' environment, you will never affect the calling program's directory. Which is partly why I had a fallback for creating tarballs: fork a new child process, chdir in that child, leaving the parent unchanged, and then exec tar. Meanwhile, the parent would wait for the child, and not need to touch the directory. Yes, I know, I could just chdir back. But, for some reason, this way made me less likely to forget. Probably because it was so painful that I had to actually pay attention ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557879]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found