in reply to Opening a file error

Update: At first I considered your tr// statement to mean that you would be doing full security checking, but it has (rightly) been pointed out that there are huge security concerns here that it would be wrong to ignore. Read KM's reply below

1) In this line:

if (!(open (OUT, ">$_"))) { print "Error opening file $_ for writing."; exit 0; }
Try this:
if(!open(OUT, ">$_")){ print "Error opening file $_ for writing: $!"; # $! holds the error mesg exit 0; }
The more tradional way to write this is:
open(OUT, ">$_") or die "Can't open $_ for writing: $!";
Since you are using this in a CGI program, it'd be a good idea to use perlfunc:warn or perlfunc:die too. (See the module CGI::Carp for http friendly die and warn functions)

But it doesn't seem to get to this point, so this isn't the source of the troubles. 2) some more information would be useful. You have the line:

print ">$_";
Does it get printed? (Note that print statements might occur but not get printed if they are in the buffer when the program gets confused. Either use warn statements, or set $|=1, which will autoflush any statements in the buffer).

Replies are listed 'Best First'.
RE: Re: Opening a file error
by KM (Priest) on Jun 09, 2000 at 18:27 UTC
    This reply didn't mention two other things with the original code that should be pointed out. First, you are not using -T (read perlsec and perlrun). Second, you aren't using -w or strict (perldoc strict and perlrun). You should NEVER NEVER NEVER NEVER create a file based on raw user input which you are doing. This is BAD BAD BAD. Learn now before you learn the hard way. You seem to use tr// to help make this safe, but it isn't the best way, and safer programming is better programming. Look at Untaint.pm for untainting of the input and read perlsec, twice. Also, I see no reason why you are using $_. Also, please skim this node but really pay attention to the comments I made on that node following it. It deals with file locking. You have no file locking here, and possibly do not need it, however you should take a look and familiarize yourself with it if you are using text files and not a DB.
    swiftone is right about looking at CGI::Carp. It is a great tool for debugging CGI, just add this in your script:

    use CGI::Carp qw(fatalsToBrowser);

    and you will get more useful error messages to the browser, most of the time at least :)

    Cheers,
    KM

RE: Re: Opening a file error
by Anonymous Monk on Jun 09, 2000 at 22:52 UTC
    Why are you using c-style comments? If these are in your code, then there's your trouble.