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

why doesn't this code work?
$TextFile = "script.pl"; use Cwd; chdir("$ENV{DOCUMENT_ROOT}/some_directory") or die $!; my $dir = getcwd; open (DAT,">${dir}${TextFile}") or die $!; print DAT ("$ENV{DOCUMENT_ROOT}/cgi_bin/$text\n") or die $!; close(DAT) or die $!;
There's no server error, but the file is not written
to another directory.

Lisa.

Replies are listed 'Best First'.
Re: output to another directory
by frankus (Priest) on Aug 09, 2002 at 11:20 UTC
    Firstly, try some sanity checks; testing the variables contain the expected values.
    Data::Dumper is great for this, if you've not encountered it before.
    Check that the paths to the files are feasable
    Then put file tests in. -e -f -d, that kinda stuff.

    Your script seems rather morbid. Try focussing more on living than dying.

    --

    Brother Frankus.

Re: output to another directory
by dda (Friar) on Aug 09, 2002 at 11:17 UTC
    Add 'use CGI::Carp qw(fatalsToBrowser);' to your script and you will see 'die $!' statement results in your browser.

    Most probably, it is directory permissions issue.

    --dda

Re: output to another directory
by demerphq (Chancellor) on Aug 09, 2002 at 12:05 UTC
    Im guessing that your problem is this
    open (DAT,">${dir}${TextFile}") or die $!;
    because $dir doesnt end with a path seperator. getcwd returns the path _without_ a trailing seperator:
    D:\Development>perl -MCwd -e "print getcwd" D:/Development D:\Development>perl -MCwd -e "$f='test.pl'; $p=getcwd; print \"$p$f\"" D:/Developmenttest.pl
    So I bet if you look around youll find a bunch of files that start with the directory name you thought you were writing to in those directories parents.

    To verify that this is correct (im 99.999999 certain it is) just do

    die "${dir}${TextFile}";
    before your open and then have a look at your server logs.

    HTH

    BTW, please dont use pre tags in your posts. And if you do please close them correctly. :-)

    Yves / DeMerphq
    ---
    Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)