Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How solve this error

Reply
Thread Tools

How solve this error

 
 
mukunthini@gmail.com
Guest
Posts: n/a
 
      04-23-2007
I'm new to Perl and am trying to get a simple counter script to work.
It seems to be able to read the counter.dat file ok but I can't get
it to write the new file count to the file. When im executing the
script in command line it's working well and updating the couner.dat
also to new value. but when im running the file in web browser it's
reading the counter value.The counter get's incremented just fine, But
I cannot get the thing to write back out to the counter.dat file with
the new value. My file permissions seem to be ok. It reads the
counter.dat file but will not write to it. and in the error log its
giving the following error
"hi.pl: Cannot open for writing: Permission denied at /var/www/cgi-
bin/hi.pl line 13."
im using Fedora linux, apache 2.0 and perl 5.6.2 .......and mozilla
firefox browser to execute this file
Anybody ever seen this happen?
here is the code:

#!/usr/local/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
print "Content-type: text/html\n\n"; # Web page as default output
open (COUNT, "counter.dat"); # Open for read using COUNT file
handle
$hitcount = <COUNT>; # Read in file to get current count
value
close COUNT; # Close the data file
$hitcount = $hitcount + 1; # Increment count value
open (COUNT, "> counter.dat") or warn "Cannot open $file for writing:
$!"; # Open data file for write
flock(COUNT, 2);
print COUNT "$hitcount"; # Write updated count value
close COUNT; # Close the data file
print "$hitcount"; # Display updated count on the page
exit;

thankx in advance.....!
regards
mukunthini

 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      04-23-2007
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I'm new to Perl and am trying to get a simple counter script to work.
> It seems to be able to read the counter.dat file ok but I can't get
> it to write the new file count to the file. When im executing the
> script in command line it's working well and updating the couner.dat
> also to new value. but when im running the file in web browser it's
> reading the counter value.The counter get's incremented just fine, But
> I cannot get the thing to write back out to the counter.dat file with
> the new value. My file permissions seem to be ok. It reads the
> counter.dat file but will not write to it. and in the error log its
> giving the following error
> "hi.pl: Cannot open for writing: Permission denied at /var/www/cgi-
> bin/hi.pl line 13."


In view of that error message, what makes you think your permissions
are ok?

> im using Fedora linux, apache 2.0 and perl 5.6.2 .......and mozilla
> firefox browser to execute this file
> Anybody ever seen this happen?
> here is the code:
>
> #!/usr/local/bin/perl


No "strict"? No "warnings"?

> use CGI::Carp qw(fatalsToBrowser);
> use CGI qw/:standard/;
> print "Content-type: text/html\n\n"; # Web page as default output
> open (COUNT, "counter.dat"); # Open for read using COUNT file
> handle


Why aren't you checking the result of open()?

> $hitcount = <COUNT>; # Read in file to get current count
> value
> close COUNT; # Close the data file
> $hitcount = $hitcount + 1; # Increment count value
> open (COUNT, "> counter.dat") or warn "Cannot open $file for writing:
> $!"; # Open data file for write


Here you *are* checking the success of open(), but the error message
is misleading. You are opening "counter.dat", but in the error
message you refer to "$file", which is nowhere set in your code.

[rest of code snipped]

Correct your code and go back to permission checking, that is
likely the problem.

Anno
 
Reply With Quote
 
 
 
 
mukunthini@gmail.com
Guest
Posts: n/a
 
      04-25-2007
Dear Anno,
Thaks alot for your reply
i've changed the codes as you mentioned, when i run in web browser it
is displaying "1". that is from the last print cmd. under error log /
var/log/httpd the following 2 errors displayed.
[Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
21:31:52 2007] hi.pl: Cannot open counter.dat for reading: No such
file or directory at /var/www/cgi-bin/hi.pl line 7.
[Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
21:31:52 2007] hi.pl: Cannot open counter.dat for writing: Permission
denied at /var/www/cgi-bin/hi.pl line 14.
when i executed from terminal it gives this error " [Wed Apr 25
21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
file or directory at hi.pl line 7 " and the new file "counter.dat" is
created under working directory and "1" is stored in that file.
Again if i execute the same form web browser now it is dispalying "2"
and in the log only writing error displayed.so it is reading from the
file, not writing the incremented value. when i execute again the same
value "2" is displaying in web browser.
But when i executing from terminal is working reading and wring new
value to that file.

#!/usr/local/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;
print "Content-type: text/html\n\n";
use strict;

open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
reading: $!";
my $hitcount = <COUNT>;
close COUNT;

$hitcount = $hitcount + 1;

open (COUNT, "> counter.dat") or warn "Cannot open counter.dat for
writing: $!";
flock(COUNT, 2);
print COUNT "$hitcount";
close COUNT;

print "$hitcount";

exit;

i have given read and write permission to both file and the directory
(cgi-bin and counter.dat) (chmod 777)....

So, is this error happened due to apache configuration mistake? do i
need to change the httpd.conf file?
or is that web browser (mozilla) doesnt have permission to write file?
how can i overcome this? pls help me....

regards,
Mukunthini



 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      04-25-2007
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Dear Anno,
> Thaks alot for your reply


Which reply do you mean? It's been almost three days. In the
meantime, I've read hundreds of questions similar to yours
and answered a few. Pleas quote some context when you reply.

> i've changed the codes as you mentioned, when i run in web browser it
> is displaying "1". that is from the last print cmd. under error log /
> var/log/httpd the following 2 errors displayed.
> [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
> 21:31:52 2007] hi.pl: Cannot open counter.dat for reading: No such
> file or directory at /var/www/cgi-bin/hi.pl line 7.


Okay, so the file doesn't exist where it is expected. You should
check for that anyway and react accordingly.

> [Wed Apr 25 21:31:52 2007] [error] [client 127.0.0.1] [Wed Apr 25
> 21:31:52 2007] hi.pl: Cannot open counter.dat for writing: Permission
> denied at /var/www/cgi-bin/hi.pl line 14.


Clearly, whoever is running the web server doesn't have write
access to that directory.

> when i executed from terminal it gives this error " [Wed Apr 25
> 21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
> file or directory at hi.pl line 7 " and the new file "counter.dat" is
> created under working directory and "1" is stored in that file.


> Again if i execute the same form web browser now it is dispalying "2"
> and in the log only writing error displayed.so it is reading from the
> file, not writing the incremented value. when i execute again the same
> value "2" is displaying in web browser.
> But when i executing from terminal is working reading and wring new
> value to that file.
>
> #!/usr/local/bin/perl
> use CGI::Carp qw(fatalsToBrowser);
> use CGI qw/:standard/;
> print "Content-type: text/html\n\n";
> use strict;
>
> open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
> reading: $!";
> my $hitcount = <COUNT>;
> close COUNT;
>
> $hitcount = $hitcount + 1;
>
> open (COUNT, "> counter.dat") or warn "Cannot open counter.dat for
> writing: $!";
> flock(COUNT, 2);
> print COUNT "$hitcount";
> close COUNT;
>
> print "$hitcount";
>
> exit;
>
> i have given read and write permission to both file and the directory
> (cgi-bin and counter.dat) (chmod 777)....


A risky thing to do. Apparently it hasn't had the desired effect
because the web server still doesn't have write access.

> So, is this error happened due to apache configuration mistake? do i
> need to change the httpd.conf file?


That is way off topic in clpm. I don't know.

> or is that web browser (mozilla) doesnt have permission to write file?
> how can i overcome this? pls help me....


The *browser* has nothing to do with it. CGI happens on the web
server.

You'll have to discuss this in a news group that is about web server
configuration. It's outside Perl territorry.

Anno
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> it gives this error " [Wed Apr 25
> 21:51:00 2007] hi.pl: Cannot open counter.dat for reading: No such
> file or directory at hi.pl line 7.
>
> open (COUNT, "counter.dat") or warn "Cannot open counter.dat for
> reading: $!";


You're not using an absolute pathname for the file.
Since the CGI standard does _not_ specify what the current working directory
will be while the script is running, you need to use the full name.

my $file = "/home/mukinthini/public_html/counter.dat";
open my $COUNT,'<',$file or warn "Cannot open $file for reading: $!";

-Joe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Trying to solve a python/mechanize "error 500" http error bruce Python 0 07-21-2008 08:19 PM
Trying to solve a python/mechanize "error 500" http error bruce Python 0 07-21-2008 08:18 PM
how can I solve Common Language runtime Error Ajith Nair ASP .Net 0 08-17-2005 08:17 AM
How do i solve this error? Miguel Dias Moura ASP .Net 2 12-09-2004 03:15 PM



Advertisments