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

Hi monks,

I am back with my queries. The last query gave me fantastic replies and I have been able ot make much progress.

Now I want to write the results of my quiz into a file - "Results.5"- which is directory which is not in the cgi-bin directory but lies in c:\quiz.

I am using the following code, but I am not able to add the results string "Class 5" (which I am able to write when I am in the cgi-bin directory. I am using windows XP

#!/usr/bin/perl use CGI qw(:standard); chdir("../../quiz"); my $outfil = ">>Results.5"; flock(OUT, 2); seek(OUT, 0, 2); print OUT "Class 5"; close(OUT);
Where am I making the mistake. Please help. Thanks in advance for reading the mail.

Replies are listed 'Best First'.
Re: Writing to a file which is external to cgi-bin directory
by ikegami (Patriarch) on Jul 21, 2009 at 17:47 UTC

    You forgot to open OUT.

    And please use constants instead of magical numbers. Replace

    seek(OUT, 0, 2);
    with
    use Fcntl qw( SEEK_END ); seek(OUT, 0, SEEK_END);

    Update: Added the comment about using meaningful constants.

Re: Writing to a file which is external to cgi-bin directory
by Old_Gray_Bear (Bishop) on Jul 21, 2009 at 18:17 UTC
    The problem is that you didn't include the two magic lines:
    use strict; use warnings;
    If you had you would have seen these indications that Something Was Not Right(tm):
    Bear> perl open.txt flock() on unopened filehandle OUT at open.txt line 9. seek() on unopened filehandle OUT at open.txt line 10. print() on unopened filehandle OUT at open.txt line 11.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Writing to a file which is external to cgi-bin directory
by jethro (Monsignor) on Jul 21, 2009 at 18:32 UTC
    Are you sure that the quiz directory is two directories up from the current directory of apache? A quick google search seems to indicate that it isn't guaranteed that apache sets the current directory to the directory in which the script is

    If in doubt, better use an absolute path i.e. chdir('c:/quiz');

      Thanks for the replies from ikegami - yes I have added the code - seek(OUT, 0, SEEK_END); - instead of constants. Yes jethro - the quiz file is two levels up and located at "..\..\Quiz"

      After adding use strict; use warnings; as suggested by Old Gray Bear the error messege like - on unopened filehandle - appears.

      So where do I go from here ?

Re: Writing to a file which is external to cgi-bin directory
by scorpio17 (Canon) on Jul 22, 2009 at 14:13 UTC

    You're writing a cgi-script to give a quiz?

    Sorry to be a wet blanket here, but have you considered what happens when:

    • A quiz taker clicks the reload button, over and over?
    • A quiz taker clicks the back button (maybe more than once), then submits a new answer?
    • A quiz taker clicks the back button, then the reload button?

    If any of these possibilities give you trouble, consider using CGI:Session to maintain state, and save state data server-side in a database like mysql.

    ps: if you think this is easy - you're doing something horribly wrong.