Gossamer Forum
Home : General : Perl Programming :

Need help w/ readin' & writin' to files

Quote Reply
Need help w/ readin' & writin' to files
I've got the following bit of code, but I can't seem to get it to work reliably...

Code:
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI qw/:standard/;

my $FileName = "ADD.DEL";

my $FileAge = -M $FileName;
if ($FileAge < 1.0) {

open(CHECK, "<count.ct") or die "Where be me count file, mon \n";
my @text = <CHECK>;
close(CHECK);
my $count = @_;


if ($count eq "0") {

print "content-type: text/html\n\n";
print '<html><body><h1>Get File</h1><br>';
print '</body></html>';
}

if ($count eq "1") {

print "content-type: text/html\n\n";
print '<html><body><h1>Dont Get File</h1><br>';
print '</body></html>';

}
}

Any pointers on where my syntax is screwed up?
Right now it only returns the "get file" part regardless of what the count file is set to.

The File Age bit works okay, the problem I have is getting it to recognize the difference between a "1" and a "0".

My next step was to add something like:

Code:
open(CHECK, ">count.ct") or die "I canna be a writin lassie \n";
select(CHECK);
print "1";
close (CHECK);

I added this to the "0" part of the if/then statement, and it opened the count file and wrote in a "1" but I got an ISE.

Here is what the overall goal is, but I really want to learn *why* I'm doing what I'm doing:

1. Script Runs
2. Checks age (in days) of another file on the same server
3. If age in days is more than 1 day old
4. Check count file
5. If count file contains 1, stop and run rest of script
6. If count file contains 0, open count file write a 1 to it
7. AND then FTP to remote server and check date of remote files, if remote files have same date as local files, stop and run rest of script
8. If remote files have different date than local file retrieve from remote server, open count file, write a 0 to it and run rest of script.

(I've got the ftp part to work and the age of the file part to work.)

Any help is really appreciated!

.

Last edited by:

Watts: Sep 24, 2003, 3:51 PM
Quote Reply
Re: [Watts] Need help w/ readin' & writin' to files In reply to
Hi Watts,

This bit could be the problem:

Code:
my $count = @_;

should be:

Code:
my $count = @text;

My guess is that @_ is empty thus returning 0. Another thing is that you are using the string comparison instead of numeric comparison in your if's. Try this instead:

Code:
if ($count == 0)

# or even:
if ($count)

#or possibly:

if (@text) {
}
else {
}

See if that gets you any where.

~Charlie
:wq
Quote Reply
Re: [Watts] Need help w/ readin' & writin' to files In reply to
open() defaults as readonly, so "<" isn't needed.

If you want to know the actual reason why a file isn't read/written, you should include $! in your error message.

You are assigning $count to @_, which is undefined, when you should be assigning it to @text. You should chomp() the variable to remove the line break if it exists. you can simplify all that into a single line:

Code:
chomp(my $count = <CHECK>);

And because $count is a number and not a string, you should really do:
Code:
if ($count == 0) {
or you'll get odd results on anything other than equality tests.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Need help w/ readin' & writin' to files In reply to
hehe... just realized I was a little late posting.

Philip
------------------
Limecat is not pleased.
Quote Reply
Re: [fuzzy logic] Need help w/ readin' & writin' to files In reply to
In Reply To:
hehe... just realized I was a little late posting.

Better late than never Tongue

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Watts] Need help w/ readin' & writin' to files In reply to
I would suggest getting hold of a good perl debugger like Open Perl IDE.
http://open-perl-ide.sourceforge.net/

Really easy to use and will save you from a lot of eye strain when it comes to tracking down those elusive syntax errors.

Bob

http://totallyfreeads.com.au
Quote Reply
Re: [lanerj] Need help w/ readin' & writin' to files In reply to
Thanks for the pointers, guys... I'll give them a whirl and see what shakes out. I really appreciate the help.