CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Reading and writing to a file

 

First page Previous page 1 2 Next page Last page  View All


Maestro
New User

May 10, 2009, 11:17 AM

Post #1 of 27 (3699 views)
Reading and writing to a file Can't Post

Argh, im at my wits end.

I just wanna try a basic script

where there is a txt file with a 0 in it.

every time the script runs it calls the value on the txt file adds 1 to it and then saves it.

Then next time it calls 1 adds 1 and saves it etc

Problem is, i dont really understand how to call data from a txt file, ive tried a few things to no avail =/


KevinR
Veteran


May 10, 2009, 9:07 PM

Post #2 of 27 (3696 views)
Re: [Maestro] Reading and writing to a file [In reply to] Can't Post

Use Tie::File, it comes with perl.

http://perldoc.perl.org/Tie/File.html
-------------------------------------------------


alex_v2
User

May 11, 2009, 5:09 PM

Post #3 of 27 (3689 views)
Re: [Maestro] Reading and writing to a file [In reply to] Can't Post

If you are using this for a page counter (which is the only thing that makes sense to me) I would recommend you you did it in a DB and not a file if you are calling it from a web server. Again this is advice without knowing why you want to do this.

-Alexander


Punakuono
Novice

May 13, 2009, 4:36 AM

Post #4 of 27 (3681 views)
Re: [KevinR] Reading and writing to a file [In reply to] Can't Post

I'm trying to write to a file some data, any data to be honest, but my perl script doesn't seem to find the file I want to write to. I get: "No such file or directory". My script is:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";

open (FILE, >myfile.txt or die $!);
print $!;
print FILE "Hello\n";
close FILE;

And that file myfile.txt is in the same folder with my script file and the script has execution rights and has permissions to read, write and execute to that .txt file. I'm using debian 5.0 and perl 5.10.0. That script worked when using perl 5.8.8 on os x. Any help would be appreciated cause I'm kind of stuck with this. I've tried absolute path to that as well without any success. Same with placing that text file into different folders with the same effect. =D I know it doesn't make much difference.


FishMonger
Veteran / Moderator

May 13, 2009, 6:20 AM

Post #5 of 27 (3678 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Change:

Code
open (FILE, >myfile.txt or die $!);


To:

Code
open(FILE, '>', 'myfile.txt') or die $!;



alex_v2
User

May 13, 2009, 7:27 AM

Post #6 of 27 (3675 views)
Re: [FishMonger] Reading and writing to a file [In reply to] Can't Post

Yeah the open statement can be written many different ways. My preferred method is as follows:

#!/usr/bin/perl
print qq(Content-Type: text/html\n\n);

open (FILE, ">myfile.txt") || die $!;
print FILE "Hello\n";
close(FILE);

Also the print $! line you had is not necessary as the die will print $! to STDERR.

-Alexander


KevinR
Veteran


May 13, 2009, 12:18 PM

Post #7 of 27 (3673 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post


In Reply To
I'm trying to write to a file some data, any data to be honest, but my perl script doesn't seem to find the file I want to write to. I get: "No such file or directory". My script is:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";

open (FILE, >myfile.txt or die $!);
print $!;
print FILE "Hello\n";
close FILE;

And that file myfile.txt is in the same folder with my script file and the script has execution rights and has permissions to read, write and execute to that .txt file. I'm using debian 5.0 and perl 5.10.0. That script worked when using perl 5.8.8 on os x. Any help would be appreciated cause I'm kind of stuck with this. I've tried absolute path to that as well without any success. Same with placing that text file into different folders with the same effect. =D I know it doesn't make much difference.


use the warnings pragma in the future:


Code
use warnings;


It would have at least warned you about unquoted strings, which is a bad habit you want to break now.
-------------------------------------------------


Punakuono
Novice

May 13, 2009, 10:55 PM

Post #8 of 27 (3663 views)
Re: [KevinR] Reading and writing to a file [In reply to] Can't Post

Now I have:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";

use warnings;
open (FILE, ">ssndata.txt") || die $!;
print "$!\n<br>";
print FILE "Hello\n";
close FILE;

And now I'm just getting "Inappropriate ioctl for device" to my web browser and that's all error messages I'm getting. Nothing more on servers error.log either. Apparently something happened. =P


alex_v2
User

May 13, 2009, 11:32 PM

Post #9 of 27 (3661 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Things to note / check.

If you are calling this script from a web server you should use the full path you wish to write the file path not a relative one.

i.e. 'open(FILE, '>/usr/local/apache/htdocs/ssndata.txt') || die $!;

second make sure the web server user has permissions to write to the location you are attempting to write the file too.

Again the print $! line is not needed. Yes it will show you there error in you browser instead of having to look in the log file but if you are coding a web app you should be tailing the error logs at all times anyways.

Side note: This is a complete style choice which is why it is just a note. use warnings; is just fine and works but can also be done by changing your opening line to the following:

#!/usr/bin/perl -w

Again this is just another way to do the same thing and is totally a matter of person style choice.

-Alexander


Punakuono
Novice

May 14, 2009, 12:36 AM

Post #10 of 27 (3659 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

The script and the file are both in the cgi-bin and the server has been configured the same way than with the server where that script actually does work. Is there some difference with os x and debian with default permissions and giving the reading, writing and execution rights to the proper files just isn't enough? I've tried absolute path to the file as well but it doesn't seem to make a difference. And I am tailing the error log all times. Wink


alex_v2
User

May 14, 2009, 12:55 AM

Post #11 of 27 (3656 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Well if it is an apache server and the install is default then the server is running as nobody so the file permission would have to be 777. You can just change this in the httpd.conf and have the server run as whoever owns the directory. DO NOT do this if the directories are owned by root.

If the directories are not owned by root then change the owner and group to the user that owns the web server directories. If they are owned by root change them to another user and then update the httpd.conf accordingly.

Please don;t take offense because as I read this I think I kinda sound like I am speaking to you like someone who knows nothing but really I'm just trying to give as much information as possible since I do not know your familiarity with *nix systems and apache web servers. If this is not your problem please come back and let me know and I will try to be of further help.

-Alexander


Punakuono
Novice

May 14, 2009, 1:16 AM

Post #12 of 27 (3652 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

It is apache server and I am noob with this stuff. Been working with apache and os x for three weeks or so. Tailing was just something I learned quite at the beginning. I'm not offended. Just trying to get my head around the problem. Tongue

And changing the directory ownership to the one apache was running, which actually was www-data for some reason actually did fix the problem. Thank you. I had tried to put the file permission to 777 but that hadn't worked either. Laugh


KevinR
Veteran


May 14, 2009, 1:43 AM

Post #13 of 27 (3651 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post


In Reply To
Side note: This is a complete style choice which is why it is just a note. use warnings; is just fine and works but can also be done by changing your opening line to the following:

#!/usr/bin/perl -w

Again this is just another way to do the same thing and is totally a matter of person style choice.

-Alexander


Actually its not a mere style choice. the warnings pragma is much more flexible where the -w switch is an all-or-nothing option. Some reading:

http://perldoc.perl.org/perllexwarn.html
-------------------------------------------------


Punakuono
Novice

May 14, 2009, 2:09 AM

Post #14 of 27 (3647 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

It appears to have stopped working... I'm having the same settings as I had when it did work and now it's complaining about not having permissions and when I give the right permissions to it I get "Inappropriate ioctl for device"... I don't understand what that message means... It feels a bit like "error occurred, please investigate".


KevinR
Veteran


May 14, 2009, 2:18 AM

Post #15 of 27 (3646 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post


In Reply To
It appears to have stopped working... I'm having the same settings as I had when it did work and now it's complaining about not having permissions and when I give the right permissions to it I get "Inappropriate ioctl for device"... I don't understand what that message means... It feels a bit like "error occurred, please investigate".


Unfortunately the error is coming from the operating system, not perl. I don't understand what the exact cause of that error is, but often operating sytem errors are quite generic and hard to pin down. Hopefully another member has some experience with that error.
-------------------------------------------------


Punakuono
Novice

May 14, 2009, 2:20 AM

Post #16 of 27 (3643 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

Ideas about why "Inappropriate ioctl for device" re-appeared?


alex_v2
User

May 14, 2009, 2:27 AM

Post #17 of 27 (3642 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Well everything I have read about your error points to an inability to read the file either because it can not find it or does not have permissions to it.

Seems odd that it worked for a bit then stopped. I would just triple check all the ownership prives of the file and file system. Remember too that some times you can have issues where it may not let you write to a file if you don't also have write and/or read permission to the directory.

-Alexander


KevinR
Veteran


May 14, 2009, 2:28 AM

Post #18 of 27 (3642 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Is the file on your local server? Can you open it like any other file if it is?
-------------------------------------------------


alex_v2
User

May 14, 2009, 2:32 AM

Post #19 of 27 (3641 views)
Re: [KevinR] Reading and writing to a file [In reply to] Can't Post

Yes Kevin you are write in that it is more flexible but if you put just use warnings in the top of your file it will act like just using the -w option with few exceptions. Only if you tell it to use certain warnings or scope it does it function differently then -w.

But thanks for the info again just years of habit. I have worked with some amazingly brilliant people in my days and the one thing every single one of them tools me was if my code I was coming to them with questions did not have -w I better not ask haha. But I appreciate the feed back since though I have used perl for a very long time I am still very much a student of it as well.

-Alexander


KevinR
Veteran


May 14, 2009, 2:45 AM

Post #20 of 27 (3639 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post


In Reply To
Yes Kevin you are write in that it is more flexible but if you put just use warnings in the top of your file it will act like just using the -w option with few exceptions. Only if you tell it to use certain warnings or scope it does it function differently then -w.

-Alexander


-w is a shotgun, the warnings pragma is more of a surgical instrument.
-------------------------------------------------


Punakuono
Novice

May 14, 2009, 3:39 AM

Post #21 of 27 (3635 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

All files are in the server and not over in somewhere else... I did change the location of the .txt file, but it worked after that for a while. I guess it does have something to do with permissions and ownership... It worked and I wrote another script doing basically exactly the same with the first one and it then complained something about permissions and then I did something that broke the first script and the second one ain't working either. Crazy


Punakuono
Novice

May 14, 2009, 4:01 AM

Post #22 of 27 (3633 views)
Re: [KevinR] Reading and writing to a file [In reply to] Can't Post

Local server? No. The files are on external server and I'm having an ssh connection to that.


alex_v2
User

May 14, 2009, 8:03 AM

Post #23 of 27 (3626 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

Is the SSH one a cron? If so is it running under the cron of the same user as the web server?

-Alexander


Punakuono
Novice

May 14, 2009, 8:31 AM

Post #24 of 27 (3622 views)
Re: [alex_v2] Reading and writing to a file [In reply to] Can't Post

I don't think it's a cron. If it is, it has been set up for me and nobody has informed me about it. Tongue


alex_v2
User

May 14, 2009, 8:38 AM

Post #25 of 27 (3621 views)
Re: [Punakuono] Reading and writing to a file [In reply to] Can't Post

So you are sshing the files over to the server by hand? when you do this are you the same user that the web server is using? Forgive me I didn't sleep much last night so I might be slower to catch on today haha.

-Alexander

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives