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

I'm using the following subroutine to write to a textfile.
Except it's not writing to my textfile. Any ideas why?
Thanks.
 
sub recordVote { my $question_id = $_[0]; my @qlist = ""; my $i = 0; foreach $i (0..4) { $qline ="$id[$i]|$question[$i]|$answer1[$i]|$answer2[$i]|$tally1[$ +i]|$tally2[$i]\n"; push @qlist, $qline; } #print "<!--\@qlist is:\n@qlist-->"; open (POLLWRITE,"+< D:/Inetpub/wwwroot/j14/include/special_poll.tx +t") or die "couldn't open poll file"; print POLLWRITE @qlist or die "Couldn't write to poll file" +; close(POLLWRITE) or die "Couldn't close poll file aft +er writing"; }

Replies are listed 'Best First'.
(ar0n) Re: write to textfile
by ar0n (Priest) on Apr 09, 2001 at 06:21 UTC
    Include the $! variable in you die statements. It may make you wiser:
    ... die "Shuffling off mortal coil: $!\n";


    [ar0n]

Re: write to textfile
by Albannach (Monsignor) on Apr 09, 2001 at 08:36 UTC
    There are a number of things here that you probably don't mean to be doing, but for starters:

    • Why do create a variable (my $question_id = $_[0];) but never use it again?
    • Assigning "" to an array does not give you an empty array, it gives you an array with one null element. You can assign () to an array to make it empty, but in this case it is a new variable and so the assignment is not needed (arrays come empty from the factory).
    • Opening with '+<' means read/write access which is not too useful with a text file unless you have a fixed line length and are keeping track of your position (and you are doing both reading and writing which you aren't). You probably just want '>' which truncates the file if it exists and opens it for output only.

    If you need more help, then you should supply more context such as what exactly does happen (with the benefit of using $! as ar0n suggested), and what is in all the global variables that this subroutine is using.

    --
    I'd like to be able to assign to an luser

Re: write to textfile
by BMaximus (Chaplain) on Apr 09, 2001 at 08:37 UTC
    Hi Squeeky, If your just writing to the file and not reading it at the same time you may just wish to open the file with:
    open(POLLWRITE, ">>D:/Inetpub/wwwroot/j14/include/special_poll.txt) or + die "Can't open file: $!\n"; # do stuff here close(POLLWRITE);

    What this does is open the file in append mode so that the file contents don't get clobbered. Theres no reason to open a file for read and write when all your doing is writing to it.

    Good luck my brother,
    BMaximus