in reply to Re: opening new file in different directory
in thread opening new file in different directory

I think this isn't the answer because I think (which I shd have spelled out) the current directory is /home/main/cgi-bin. The reason I'm a bit equivocal is that I do not explicitly set it to be the current directory; it's jus the directory where my script resides. Is that the problem? Why?

§ George Sherston

Replies are listed 'Best First'.
Re (3): opening new file in different directory
by VSarkiss (Monsignor) on Oct 23, 2001 at 19:24 UTC

    Two things: First, depending on the server configuration, the script may or may not be executing in the directory in which the file is installed. It's not where the script is installed that matters, but the current directory of the Perl process that's executing your script.

    Second, more importantly: if you depend on a particular path, don't assume you're starting there, chdir to that directory explicitly. It guarantees your paths and has negligible overhead. If you're worried about spelling out the directory name in your script, read it in from the environment or some such. But don't assume your script will always execute in the same place.

      So wd you recommend, say, having a function that "sandwiches" each file operation between a pair of chdirs? I do quite a lot of reading and writing, but I also pick up a lot of files (templates and so on). At the moment I keep all the auxiliary files in the same dir as the script and refer to them by name only, no explicit path.

      /me scratches head and wonders whether I shd use explicit file paths for *everything* I refer to from my script. Wd that be considered good practice?

      § George Sherston

        Ah, I'll give you the best answer ever in computing:

        It depends.

        Explicit paths for everything in your program sounds like overkill, since you have to maintain the paths and may end up with big headaches if you ever move stuff around.

        Personally, I'd organize the files so I wouldn't have to do a lot of chdir'ing. Then set the path once and go about the rest of the business. But I don't want to make a blanket statement about how to organize files and i/o without knowing a whole lot more about what you're doing. (Not that I want to know -- I've got enough problems of my own. ;-)

        HTH