Click to See Complete Forum and Search --> : Why is open() failing? (Perl)


Energon
02-22-2002, 07:33 PM
I hate to ask a question about IIS here, but I don't know for sure if it's ActivePerl or IIS that's being the problem.

I've got this CGI script here, and it runs dandily great on my Linux machine with Apache. But when we transfer it over to the actual IIS machine (running the latest ActivePerl build), the calls to open() fail (I'm writing some data to a file from a form). Here's the relevant portions of the script:


#! /usr/bin/perl -w
use strict;
use CGI qw(param);


#
# GLOBAL VARIABLES
#

# set this to where form results will be saved (this file must exist and be writable by the server user)
my $savefile = "C:\\InetPub\\wwwroot\\tech\\results.txt";

# the page to return to
my $returnpage = "http://www.kunaschools.org/tech/classes/default.htm";


#
# FUNCTIONS
#


# prints parameters to a file
sub write_file
{
# open the file (create it if necessary
unless(-e $savefile) {
unless(open(RESULTFILE, ">$savefile")) {
error "Could not create results file (results not saved)";
return 0;
}
} else {
unless(open(RESULTFILE, ">>$savefile")) {
error "Could not open results file (results not saved)";
return 0;
}
}

# print the results
select(RESULTFILE);

select(STDOUT);

# close the file
unless(close(RESULTFILE)) {
error "Could not close results file (results not saved)";
return 0;
}

return 1;
}


# the main function
sub main
{
# write the file and confirm if that was successful
confirmation if(write_file);
}


# print the content header and run main
print "Content-type: text/html\n\n";
main;


We had to create the file by hand, and it does see it, but the call to open errors out (I print "Could not open file" instead of "Could not create file".

The system is on Fat32, so I can't see how it's a permission problem, so is there something about Perl not liking open() in Windows, or did we misconfigure IIS and script permissions?

takshaka
02-22-2002, 11:34 PM
Put $! in your error message; perl will tell you why the open failed.

btw--while it doesn't bother me particularly, the unless/else combo is generally frowned upon.

Energon
02-25-2002, 01:01 AM
So it's generally best to use if/else instead?

I'll try what you sugessted on Monday, thanks! :)

TheLinuxDuck
02-25-2002, 10:53 AM
Energon:

In this case, since the code is calling a subroutine, I personally would make the subroutine exit if the open fails. Something like:


sub write_file($)
{
my($savefile) = shift or (print "no savefile received" and return -1);
 
open FILE, ">>$savefile" or (print "write to file \'$savefile\' failed: $!\n" and return -1);
 
# write info to file
close FILE or (print "close failed: $!\n" and return -1);
}

takshaka
02-25-2002, 04:08 PM
Originally posted by TheLinuxDuck:
In this case, since the code is calling a subroutine, I personally would make the subroutine exit if the open fails.

Er, isn't that what he's doing? :p

Originally posted by Energon:
So it's generally best to use if/else instead?

Functionally, it doesn't matter. Some people, however, are uncomfortable seeing an else block after an unless.

TheLinuxDuck
02-25-2002, 06:53 PM
Originally posted by takshaka:
<STRONG>Er, isn't that what he's doing?</STRONG>

My fault. I didn’t read the code close enough.

Energon
02-25-2002, 07:28 PM
hmm... it says Permission denied. Looks like it's back to messing around again.

Thanks for the help, I appreciate it. :)