Home > Archive > PERL CGI Beginners > May 2004 > writing to a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Perldiscuss - Perl Newsgroups And Mailing Lists 2004-05-22, 11:32 am |
| Hi
I have a very simple cgi script that I have put onto a webserver. I want
to write to a log file when it is run. My problem is that the open file
command always fails (and I get the "openFailed" message in my browser).
(even if i try opening for input it fails). I have created the file called
log.dat in the same folder as the script (cgi-bin) and set attributes to
read/write using chmod.
Is there any easy way I can find out why the file cant be opened (is it
permissions?). Do I need to fully qualify the filepath? if so how do I do
that? Is it because I'm not allowed to have log files in the cgi-bin
folder?
Any help for an absolute beginner appreciated.
steve
#!/usr/local/bin/perl
$Msg = "";
if( ! open (LOGFILE, ">> log.dat") ){
$Msg = "OpenFailed";
}
else {
$Msg = "OpenWorked";
#print LOGFILE $Msg;
close (LOGFILE) ;
}
print "Content-type: text/html\n\n";
print <<"EOF";
<HTML>
<HEAD>
<TITLE>Result</TITLE>
</HEAD>
<BODY>
<TABLE DIR=LTR BORDER>
<CAPTION>$Msg</CAPTION>
<TR>
<TD>$Msg</TD>
</BODY>
</HTML>
EOF
| |
| Wiggins D Anconia 2004-05-22, 11:32 am |
| > Hi
>
> I have a very simple cgi script that I have put onto a webserver. I want
> to write to a log file when it is run. My problem is that the open file
> command always fails (and I get the "openFailed" message in my browser).
> (even if i try opening for input it fails). I have created the file called
> log.dat in the same folder as the script (cgi-bin) and set attributes to
> read/write using chmod.
>
> Is there any easy way I can find out why the file cant be opened (is it
> permissions?).
You can get further help on what you need to know by using the $!
variable in your exit code. $! is a special variable (perldoc perlvar)
that tells you the reason why a particular action fails. See below.
Do I need to fully qualify the filepath? if so how do I do
> that? Is it because I'm not allowed to have log files in the cgi-bin
> folder?
>
Shouldn't, depends, possibly but not necessarily.
> Any help for an absolute beginner appreciated.
> steve
>
>
> #!/usr/local/bin/perl
>
use strict; #always
use warnings; #usually
> $Msg = "";
my $Msg = '';
>
> if( ! open (LOGFILE, ">> log.dat") ){
> $Msg = "OpenFailed";
$Msg = "Open failed: $!";
$Msg will now include the human readable version of why the open fails.
> }
> else {
> $Msg = "OpenWorked";
> #print LOGFILE $Msg;
> close (LOGFILE) ;
> }
>
> print "Content-type: text/html\n\n";
>
> print <<"EOF";
> <HTML>
>
> <HEAD>
> <TITLE>Result</TITLE>
> </HEAD>
> <BODY>
> <TABLE DIR=LTR BORDER>
> <CAPTION>$Msg</CAPTION>
> <TR>
> <TD>$Msg</TD>
> </BODY>
> </HTML>
> EOF
>
Give it a shot and see if that helps, if not post again...
http://danconia.org
| |
| Shirley A Gottron 2004-05-22, 11:32 am |
| Also, the first line,
> #!/usr/local/bin/perl
What if the server isn't a UNIX server, but an IIS server? Doesn't this line
have to change?
-----Original Message-----
From: Wiggins d Anconia [mailto:wiggins@danconia.org]
Sent: Thursday, May 20, 2004 10:28 AM
To: sjm26@hotmail.com; beginners-cgi@perl.org
Subject: Re: writing to a file
> Hi
>
> I have a very simple cgi script that I have put onto a webserver. I want
> to write to a log file when it is run. My problem is that the open file
> command always fails (and I get the "openFailed" message in my browser).
> (even if i try opening for input it fails). I have created the file called
> log.dat in the same folder as the script (cgi-bin) and set attributes to
> read/write using chmod.
>
> Is there any easy way I can find out why the file cant be opened (is it
> permissions?).
You can get further help on what you need to know by using the $!
variable in your exit code. $! is a special variable (perldoc perlvar)
that tells you the reason why a particular action fails. See below.
Do I need to fully qualify the filepath? if so how do I do
> that? Is it because I'm not allowed to have log files in the cgi-bin
> folder?
>
Shouldn't, depends, possibly but not necessarily.
> Any help for an absolute beginner appreciated.
> steve
>
>
> #!/usr/local/bin/perl
>
use strict; #always
use warnings; #usually
> $Msg = "";
my $Msg = '';
>
> if( ! open (LOGFILE, ">> log.dat") ){
> $Msg = "OpenFailed";
$Msg = "Open failed: $!";
$Msg will now include the human readable version of why the open fails.
> }
> else {
> $Msg = "OpenWorked";
> #print LOGFILE $Msg;
> close (LOGFILE) ;
> }
>
> print "Content-type: text/html\n\n";
>
> print <<"EOF";
> <HTML>
>
> <HEAD>
> <TITLE>Result</TITLE>
> </HEAD>
> <BODY>
> <TABLE DIR=LTR BORDER>
> <CAPTION>$Msg</CAPTION>
> <TR>
> <TD>$Msg</TD>
> </BODY>
> </HTML>
> EOF
>
Give it a shot and see if that helps, if not post again...
http://danconia.org
--
To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
For additional commands, e-mail: beginners-cgi-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Wiggins D Anconia 2004-05-22, 11:32 am |
| Please bottom post...
> Also, the first line,
>
>
> What if the server isn't a UNIX server, but an IIS server? Doesn't
this line
> have to change?
>
Careful, Unix is an OS type, IIS is an application software group.
It is my understanding (which could be very wrong) that IIS/Perl will
just ignore the line, IIS will have a pre-registered handler for the
script call, then Perl should recognize the line for what it is and skip
it, (with the small exception that flags provided on the line may be
included, such as -T).
Please test this before taking my word for it though :-)...
http://danconia.org
| |
| William McKee 2004-05-22, 11:32 am |
| On Thu, May 20, 2004 at 09:02:18AM -0000, PerlDiscuss - Perl Newsgroups and mailing lists wrote:
> Is there any easy way I can find out why the file cant be opened (is it
> permissions?).
Yes, print the error message that Perl provides in $!. For example,
eval {
open (LOGFILE, ">> log.dat") || die "Can't open log.day for writing: $!";
}
if ($@) {
$Msg = $@;
}
> Do I need to fully qualify the filepath? if so how do I do that? Is
> it because I'm not allowed to have log files in the cgi-bin folder?
I don't think so if it's in the same directory as the cgi script. These
questions all depend on how your ISP has configured the webserver.
HTH,
William
--
Knowmad Services Inc.
http://www.knowmad.com
|
|
|
|
|