Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

how to avoid chdir(directories) ?

by steph_bow (Pilgrim)
on Feb 27, 2008 at 14:36 UTC ( [id://670644]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

My scripts and my input data are not in the same directory.

Do I have to use the command chdir("path_to_the_data") ot is there a simple way to bypass it ? Thanks a lot

Replies are listed 'Best First'.
Re: how to avoid chdir(directories) ?
by moklevat (Priest) on Feb 27, 2008 at 14:41 UTC
    Can't you simply tell the script where the data file is by opening the file with a full path? Are you expecting some output file to be written to the same directory as the input data? If you need to work with several input files in different directories, you might explore File::Find as a solution.
Re: how to avoid chdir(directories) ?
by Fletch (Bishop) on Feb 27, 2008 at 14:45 UTC

    Well judging by the code sample showing what you're doing to access the input data it's obvious . . .

    Oh, never mind.

    (The answer is, of course, it depends. But since it depends on how you're using your "input data" no one will be able to say for sure. If you're for example reading files then you should be able to give either an absolute path or a relative path from wherever you're being run to open and it'll handle things just fine (the problem comes in when you may not be able to formulate a constant relative path (e.g. your code is run from several different directories that are at different depths relative to the input files so it'd require varying numbers of "../"s to get back)). If you're running other utilities via system or backticks then it depends on the ability of those programs to take relative or absolute paths (but again you'd have the same issue possibly with relative paths as if you opened them yourself).)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      backticks and system depend on the self of user who run the script. Most of the time they use /bin/sh, so they can resolve most of the path (relative path and absolve path and some shorthands)....

      Back to the first post. you can use open with both absolute path and relative path, as long as you stay with unix-like OS. But note that the use of chdir will make your code much easier to change.

Re: how to avoid chdir(directories) ?
by ikegami (Patriarch) on Feb 27, 2008 at 21:22 UTC
    I'll use the following directory tree in my examples:
    /my/prog/bin/script.pl /my/prog/data/file.txt
    • You can use a relative path to the data (e.g. /my/prog/bin> script.pl ../data/file.txt)
    • You can use an absolute path to the data (e.g. /my/prog/bin> script.pl /my/prog/data/file.txt)
    • You can run the script from the data directory (e.g. /my/prog/data> ../bin/script.pl file.txt)
    • You can even mix and match (e.g. /my/prog> bin/script.pl data/file.txt)

    I passed the path to the data as an argument, but it can be hardcoded or even dynamically constructed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-19 08:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found