Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

CGI troubles - not writing to file

by emilford (Friar)
on Jun 30, 2002 at 15:08 UTC ( [id://178370]=perlquestion: print w/replies, xml ) Need Help??

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

Monks-

I wrote a script a long while back that would allow me to update the front page of my site with a simple HTML form. The script was working perfectly, or at least as good as it needed to be, until just recently. I've tried to recall the changes that I've made to the script since then:
  1. added a hash key/value
  2. changed method="POST to method="POST" in the HTML form
Other than those two changes, I'm almost certain I have changed nothing else. Now, however, when I run the script, my front page turns up completely blank, along with all of the backups I make prior to changing the main page. What could be causing this sudden change in the proper functioning of my script? I know this is a vague questions, but I'm baffled and have no clue what to do, as everything was working perfectly a few hours ago.

A little insight into my script, I do error checking on things like opening files, so it should tell me if the error is occuring there. Nothings shows up, so I'm assuming this isn't the problem. Shouldn't these lines of code work to backup a file?
sub back_up_file { my ($index, $index_backup) = @_; # open the necessary files needed for reading and writing open (INDEX, "<$index") or die &show_error ("Unable to open $index +: $!"); open(BACKUP, ">$index_backup") or die &show_error ("Unable to open + $index_backup: $!"); # write the existing file into the backup while (<INDEX>) { print BACKUP "$_"; } # close the files close (INDEX); close (BACKUP); } # shows the error returned inside of the browser - easier debuging sub show_error { print "Content-type: text/html\n\n"; print "This is the error: <h1>$_[0]</h1>"; exit(0); }
When I check the backup file, however, it is entirely blank. Could this be a permissions issue that my host just changed? I thought about that possibility at first, but when I run a different script that functions similarly, but on a different part of the site, everything runs smoothly as it should. Any suggestions?

Replies are listed 'Best First'.
Re: CGI troubles - not writing to file
by Aristotle (Chancellor) on Jun 30, 2002 at 16:35 UTC

    Stumped here too, but some technical critique: rather than rolling your own error reporting routine use CGI::Carp; - and during development but not on the production machine, use CGI::Carp qw(fatalsToBrowser); which will report you the death message. Then in the script just die "Error here"; - that's what die is for after all. In your case, the exit(0); prevents the die from happening.

    Also, I'm almost sure it won't solve anything, but I recommend you use File::Copy over rolling your own copying function as it knows how to handle a great number of fringe conditions.

    Makeshifts last the longest.

Re: CGI troubles - not writing to file
by cjf (Parson) on Jun 30, 2002 at 15:15 UTC

    Your script works fine for me when called like so:

    my $index = 'data.txt'; my $backup = 'data2.txt'; back_up_file($index, $backup);

    It copies the data into data2.txt as expected.

    Update: Have you checked the backup file and Index files? Are they both actually empty? Do you open the files anywhere else in the script? If you post the rest of the code we can check it out.

      Yes, it seems that it should be working correctly, but for some reason it's not. My backup_index.html file turns up entirely blank. I hate when things just don't work when they seemingly should. Could another part of the script have an effect on this one function? I know it is at least getting to the back_up_file function since the file is being opened in write mode (hence being emptied out).
Re: CGI troubles - not writing to file
by emilford (Friar) on Jun 30, 2002 at 15:41 UTC
    Okay, I just wrote a quick little test script that should work as far as I can tell. I copied the functions exactly as they were in the original script, restored my index file, and then ran the script. The result, a blank backup.html file. What gives?
    #!/usr/bin/perl -w use strict; my $index = '../index.php'; # the main page + to be edited my $index_backup = '../edit/backup.html'; # the main pa +ge backup my $url = 'http://www.mywebsite.com/edit/backup.html'; &back_up_file ($index, $index_backup); &redirect ($url); # makes an exact copy of the current index file - security incase some +thing # were to go wrong when adding the news entry sub back_up_file { my ($index, $index_backup) = @_; # open the necessary files needed for reading and writing open (INDEX, "<$index") or die &show_error ("Unable to open $index +: $!"); open(BACKUP, ">$index_backup") or die &show_error ("Unable to open + $index_backup: $!"); # write the existing file into the backup while (<INDEX>) { print BACKUP "$_"; } # close the files close (INDEX); close (BACKUP); } # redirects the user to the updated index page - or wherever specified sub redirect { use CGI qw(:cgi); my $url = $_[0]; my $q = CGI->new(); print $q->redirect( -url => $url ); } # shows the error returned inside of the browser - easier debuging sub show_error { print "Content-type: text/html\n\n"; print "This is the error: <h1>$_[0]</h1>"; exit(0); }
      That's really strange, your script worked for me (perl 5.6.1, apache 1.3.20) !

      Could there be a problem with the relative paths to the files ?

      ---- kurt
        One suggestion that could solve this mystery:
        Try to catch the error of close(BACKUP):
        close(BACKUP) or show_error ("error closing [$index_backup]: $!");
        It is generally a good idea to check even close for errors.

        ---- kurt

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found