Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How could you make a script think it's running in its own directory?

by why_bird (Pilgrim)
on Mar 27, 2008 at 09:16 UTC ( [id://676660]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

Is there a way of tricking a perl script into thinking I'm running it from it's own directory, even when I'm running it from somewhere else?

For example, I have a script called backup.pl, and I want to create an alias to just type 'backup' in the shell and for that to run my backup.pl. However, the way I've written it, it needs to run in it's own directory (e.g. to create dated folders in ~/other/BACKUP/ which is where the script resides..)

I suppose I could hard-code all the file names etc in the file, but having written it badly :O (but also out of general interest in understanding how these things work) I'm wondering if there's any way to tell it that I want it to create the folders in it's own folder rather than where I run it from.

cheers, why_bird
........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

Replies are listed 'Best First'.
Re: How could you make a script think it's running in its own directory?
by Corion (Patriarch) on Mar 27, 2008 at 09:27 UTC

    I prefer File::Basename for that:

    use File::Basename; my $base = dirname $0; chdir $base or warn "Couldn't find base directory '$base'. Using current direc +tory.";

    This fails if you have weirdo symlinks pointing around. There is FindBin which goes through weird contortions to cover special cases but opens up other venues of failure cases.

      __FILE__?
        How does __FILE__ help? Would you use it with chdir?
        ........
        Those are my principles. If you don't like them I have others.
        -- Groucho Marx
        .......
Re: How could you make a script think it's running in its own directory?
by almut (Canon) on Mar 27, 2008 at 09:26 UTC
      Yeah, this is clearly a question of changing the pwd.
Re: How could you make a script think it's running in its own directory?
by why_bird (Pilgrim) on Mar 27, 2008 at 10:07 UTC
    thankyou---chdir and File::Basename was what I was looking for.
    Cheers
    why_bird
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
      There's also FindBin but I don't recommend its use, I've had troubles with it in the past (permissions issues in CGI scripts on Linux) plus, like tye said, FindBin is broken.
Re: How could you make a script think it's running in its own directory?
by Lu. (Hermit) on Mar 28, 2008 at 09:19 UTC
    You could also write a wrapper in bash or whatever shell you use and place that on your PATH.

    Something on the lines of
    #!/usr/bin/bash cd ~/other/BACKUP/ ./backup.pl
    should do the trick.

    Lu.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found