G'day all

I have written the following script, as an exercise in understanding how the DBI does (or doesn't) work.

The "mecury" database contains a table called "counter" which has one column "count". "count" currently holds the value 2. (set manually)

#!/usr/bin/perl -w # mercurus' counter script # version 0.9 # 21 April 2003 # Documentation to follow... # Before configuring this file, please run: ./configure.pl to establis +h the required mySQL table. # Modules. use strict; use CGI; use DBI; use CGI::Carp qw(fatalsToBrowser); # Added for debugging purposes. # Variables. my $db_database = "**********"; # The name of the mySQL database. my $db_username = "**********"; # The name of the mySQL user. my $db_password = "**********"; # The password for the mySQL user. # Main script. ## Connect to the mySQL database. my $dbh = &mysql_connect ($db_database,$db_username,$db_password); ## Print the current database value. my $current_count = &print_current ($dbh); ## Increment the database value. my $exit_code = &increment ($dbh,$current_count); ## Exit. if ($exit_code == 0) { exit; } else { die "Unknown error. Execution failed, but database updated.\n"; + } # Subroutines. ## Current database value printing subroutine. sub print_current { my $dbh = (@_); ## Retrieve information from the database. my $sth_1 = $dbh->prepare ("SELECT count FROM counter;"); $sth_1->execute || die "$dbh->errstr()"; while (my $fields = $sth_1->fetchrow_arrayref()) { my $current = $fields->[0]; } ## Display that information for the user. print "Content-type: text/html\n\n"; print "$current others have gathered information here.\n"; return ($current); } ## Current database value incrementing subroutine. sub increment { my ($dbh,$current) = (@_); ## Increment the previous value. my $updated = $current + 1; ## Write the new value to the database. my $sth_2 = $dbh->prepare ("UPDATE counter SET count='$updated' WHERE +count='$current';"); $sth_2->execute || die "$dbh->errstr()"; return ($exit_code); } ## MySQL database connecting subroutine. sub mysql_connect { my ($db_name,$username,$password) = (@_); ## Connect to the database with the values supplied. my $dbh = DBI->connect("DBI:mysql:$db_name","$username","$password"); return ($dbh); } # End of file.
Unfortunately, the error produced is:
Global symbol "$current" requires explicit package name at 
     /var/www/cgi-bin/hit_counter.cgi line 54.

Global symbol "$current" requires explicit package name at 
     /var/www/cgi-bin/hit_counter.cgi line 56.
As far as I can tell, the value of $current is initialised on line 49 ... or do I have scoping issues outside of the while loop ?

Can anyone point me to a solution ?
(I've read the DBI perldoc and some basic DBI tutorials from a few places - including here, to no avail...)

Thanks in advance
mercurus

edited: Sun Apr 27 19:19:56 2003 by jeffa - readmore tag, formatted error message


In reply to Initialised values that fail to initialise..? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.