I tried many ways, but failed - so I thought using a sub could be a
workaround.
See codes below.
package Store::DisplayTable;
use strict;
use DBI;
use vars qw(@ISA @EXPORT);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(displayTable);
use lib qw(./Store);
use Store::Layout;
use Store::Config qw($images $currency);
use Store::DBConnect;
use vars qw($images $currency $counter);
##################################################
# Subroutine to display queries in tabular form.
# This function expects 3 arguments: query string,
# number of rows and a list of table headings.
##################################################
sub displayTable {
print "Content-type: text/html\n\n";
my ($query, $tableHead, $session_id) = @_;
my @table_head = @$tableHead;
top();
print qq(<table border="1" width="95%"
align="center"><tr><td> </td></tr>
<tr><td>);
my $rows = $counter; # Warning of uninitialized value.
if ($rows == 0){
print qq(<p><h3>Your search yield no result:</h3><br
/> </p>);
print qq(</td></tr><tr><td> </td></tr></table>);
} elsif ($rows == 1){
print qq(<p><h3>Your search yield 1 result:</h3><br
/></p>);
print qq(</td></tr><tr><td> </td></tr></table>);
}else {
print qq(<p><h3>Your search yield $rows
results:</h3><br> </p>);
print qq(</td></tr><tr><td> </td></tr></table>);
}
# Fist of all - print a start table TAG.
print qq(<table border="1" width="95%" cellspacing="2"
cellpadding="2" align="center">);
print qq(<tr bgcolor="#5692cb">);
# Print the table headings.
print qq(<th> </th>);
foreach my $heading (@table_head) {
print qq(<th>$heading</th>);
}
print qq(<th> </th></tr>);
# Connect to database and execute query.
my $dbh = dbConnect();
my $sth = $dbh->prepare($query) || die "Can't
prepare:$query\n$DBI::errstr";
$sth->execute();
# Get the results as array.
my $x = 0;
while (my @result = $sth->fetchrow_array()){
$x++;
# Index each row: set row colour based on their position
(odd or even numbers).
if (($x % 2) == 0){
print qq(<tr bgcolor= "#e8e8e8">);
}else {
print qq(<tr bgcolor="#d0d0d0">);
}
print qq(<td width="7%">);
if ($result[0] ne " "){
print qq(<a
href="../jupiterShop/item_details.pl?item=$result[1]">
<img src="$images/$result[0]" width="70"
height="64" alt="Click on image to see product detail." /></a></td>);
}else { print qq( </td>); }
print qq(
<td><a
href="../jupiterShop/item_details.pl?item=$result[1]">$result[2]</a><br>
<font size="-1">$result[3]</font></td>
<td width="15%" align="center">$result[4]$currency</td>
<td width="15%" align="center" valign="middle"><form
action="../jupiterShop/inCart.pl"
method="post">
<input type="hidden" name="prodnr" value="$result[1]">
<input type="text" name="quantity" value="1" size="2"
maxlength="3" />
<input type="image" src="$images/cart.gif" width="42"
height="29" alt="Place item in
shopping cart"/></form>
<input type="hidden" name="session" value="$session_id">
</td></tr>
);
}
# Pass the value $x to the sub to make it available above
without the pragma "use vars".
$counter = $x;
print qq(</table>);
print qq(<table border="1" width="95%" align="center"><tr><td
height="10"> </td></tr><tr><td>);
print qq(page number: </td></tr><tr><td
height="10"> </td></tr></table>);
footer();
$sth->finish();
$dbh->disconnect();
}
sub counter{
# my $counter = shift;
return(my $counter) = shift;
}
1;
||> -----Original Message-----
||> From: Charles K. Clarkson [mailto:[EMAIL PROTECTED]
||> Sent: Saturday, April 17, 2004 2:35 AM
||> To: 'B. Fongo'; [EMAIL PROTECTED]
||> Subject: RE: How do I make a variable globally accessible without
vars
||>
||> B. Fongo <[EMAIL PROTECTED]> wrote:
||> :
||> : I get the usual warning "Use of uninitialized ." while trying
||> : to test a variable ($counter) which is initialized later in
||> : my script.
||>
||> Well that would make sense. The if statement below assumes
||> a value is in $counter. In perl undefined and 0 are both false.
||>
||> : Using the pragma use vars ($counter) makes it sticky.
||>
||> 1. Don't use this unless you are trying to create a global.
||> 2. Don't use globals.
||>
||>
||> : To avoid, I tried to work around it by passing it to a sub.
||> :
||> : my $counter = counter();
||> :
||> : if ($counter == 0){
||>
||> I don't know what you intend to accomplish with the
||> subroutine. Why not tell us what you want to do and we'll
||> help you get there.
||>
||>
||>
||> HTH,
||>
||> Charles K. Clarkson
||> --
||> Mobile Homes Specialist
||> 254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>