sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

I have two questions for any not-busy perl monks tonight, if you can help with either one I'd be very much appreciated. Firstly, I have had problems time and time again with databases over writing themselves and each time it's a different problem so if I get an old script to work it has nothing to do with why this one won't. Can someone take a quick look below and see if you can see why only the last value attempted is saved into the db? (database variable is $emails('$email') = " ")
#!/usr/bin/perl -w open( STDERR, ">>/home/sulfericacid/public_html/test/error.log" ) or die "Cannot open error log, weird...an error opening an error log +: $!"; use CGI::Carp 'fatalsToBrowser'; use strict; use warnings; use POSIX; use CGI qw/:standard/; # INIT { $| = 1 } require SDBM_File; my %emails; my $list = "list.dbm"; my $adminmail = 'admin@test.com'; my $sendmail = "/usr/lib/sendmail"; print header, start_html('Email Management'); print start_form(), table( Tr( td("Email: "), td( textfield( -name => 'email', -size => 40 ) ) ), Tr( td(), td(submit) ), ), end_form(), hr(); if ( param() ) { my $email = param('email'); if ( param('email') ne "" ) { tie %emails, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %emails ) { warn("database unsuccessful $!.\n"); } $emails{$email} = " "; print "Email address added to database!"; foreach ( sort keys(%emails) ) { print "$_ => $emails{$_}<br>"; } } else { print "Where is the email?\n"; } } print end_html();
Now I have a question, please don't give me any codes...I just need ideas on how to do this. I have a counter I made a while back but I want to track $totalhits, $hitstoday, $weekhits. How could I go about setting it up so it logs total hits, hits done today and hits done this week? I've never worked with time scripts so I'm pretty clueless. Any guidance (but NO codes please, I want to write this myself) would be so appreciated!

Thanks everyone

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: DB Ovewriting (again..)/ Counter Question
by jasonk (Parson) on Apr 06, 2003 at 06:25 UTC

    Your first script works fine, if you were checking the result of the tie, you would probably find that there is some sort of error opening the dbm file (probably permissions, since it's a CGI script, the user the cgi runs as probably doesn't have permission to write to the directory you are asking it to create the database in).

    For the second part, I would store a count of the hits for each day associated with their dates, that way you actually only store one count for each day, so when you want your script to display $hitstoday, you just display the counter for that day, when you want $weekhits, you add up the last 7 days of hits in your database and display that, and if you want $totalhits, you add up the counters for all the dates.


    We're not surrounded, we're in a target-rich environment!
      Ok, you figured out what the error was. I am so frustrated because it was a simple thing like 'the database isn't being made'. I was nearly sure it was (though I should have checked) because my or die(warn..) never mentioned anything.

      As for the counter script would I have to do something which will calculate hours into days into a week so the script can work with it?

      Thanks for your help!

      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        As for the counter script would I have to do something which will calculate hours into days into a week so the script can work with it?

        I would just store it along with the current date, if storing it in a dbm, you could just do $counters{$date}++;. That way you get a dbm with dates for keys, and counts for value. To get a count for the week, just figure out which 7 days make up the week you want data for (using Date::Calc or something similar would make it easy), then add up the counters for those 7 days. To get a total count, add up all the values.


        We're not surrounded, we're in a target-rich environment!