Gossamer Forum
Home : General : Perl Programming :

Simple Perl Problem (I think)

Quote Reply
Simple Perl Problem (I think)
Hi,

Just a simple question...

I'm just getting into Perl and am writing a simple script...I was wondering if anyone knows how I can set a variable that acts as an ID tracker/incrementer?

For example: each time a page is loaded, it would go up 1...or I'd put the variable (let's call it "$count") in a hidden input form field on a page to serve as an ID-tracking number...I know there's a simple way to do this...can anyone help out?

Thanks,
Chris

Quote Reply
Re: Simple Perl Problem (I think) In reply to
The following is a simple example from Widgetz's Review.it Mod:

Code:

open (ID, "<$db_id_file_name") or &cgierr("Error in Review. Unable to Open ID File: $db_id_file_name. Reason: $!");
$in{$db_key} = <ID> + 1;
close ID;


Then you would have to add the following hidden field in your forms:

Code:

<input type="hidden" name="ID" value="$rec{'ID'}">


Regards,

Eliot Lee
Quote Reply
Re: Simple Perl Problem (I think) In reply to
A couple of things:
a) Where is the code to write the new value to the data file?

b) why make it a form field variable? There is no need and it is bound to create false data. User A comes along and hits the page and gets a counter value of 2. Meanwhile 25 other users have hit the page and submitted the form while User A is getting coffee. User A comes back and submits the form now showing that you have had 2 hits rather than 32.

As far as the code goes, you don't need to use the & anymore if you are using ()'s and you don't need to use the < operator since reading is the default operation for opening.

If you just want a way to track page hits you could just use something like this:

open(DF, $your_data_file) or Err_Msg("[error message]: $!");
chomp(my $num = <DF>);
close(DF);
open(DF, ">$your_data_file") or Err_Msg("[error message]: $!");
print DF $num;
close(DF);

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Simple Perl Problem (I think) In reply to
Hey Gordon, did you leave out a $num++ to increment the variable? Otherwise, I concur with you.

James Hall

<a href="mailto:james@de-inc.com">james@de-inc.com</a>

ICQ: 72409850
Quote Reply
Re: Simple Perl Problem (I think) In reply to
Yah, my fingers were out of touch with my brain;)

change
chomp(my $num = <DF>);
to
chomp(my $num = <DF>) + ++$num;

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';