PDA

View Full Version : Using variables in module


netroact
03-06-2006, 04:27 AM
I made a little module to display the table I use for several scripts. Because I use different MySql statements in each script, I just want to display the table. Here is part of the module:


package DisplayTable;
require Exporter;

our @ISA=qw(Exporter);
our @EXPORT=qw(display);
our $VERSION=1.00;


####################################################

sub display
{

print "Content-type:text/html\n\n";
print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Portfolio Manager</title>

<style>
<!--
#table_title
{
text-align: center;
color: #000080;
font: 8pt arial;
}
#table
{
text-align: center;
color: #3366aa;
font: 8pt arial;
}
-->
</style>

</head>

<body>



<table border="1" bordercolor="#3366aa">
<tr bgcolor="f0f0f0">
<td height="30" align="center"><div id='table_title'><b>Date</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock Buy</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock Buy</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock EOD</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock EOD</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock Sell</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Stock Sell</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Spread</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Commission</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Confirm Fee</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Fees</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Interest</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Deposit</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Withdraw</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Securities Value</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Cash & Equiv</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Margin</b></div></td>
<td height="30" align="center"><div id='table_title'><b>Net Equity</b></div></td>
</tr>
EOF

our @fields;
our $sth2;
#loop through the recordset returned by the query
while (@fields = $sth2->fetchrow())
{

print "<tr>";
print "<td align=center><table align=center><tr>";
print "<td align=center><div id='table'>$fields[1]/$fields[2]/$fields[3]</div></td></tr></table></td>";




I am getting this error:
Can't call method "fetchrow" on an undefined value at DisplayTable.pm line 71.

I'm assuming I need to know how to pass $sth2 to the module. What do I need to do?

Thanks FishMonger!

FishMonger
03-06-2006, 06:37 PM
I've never tried to do what you're attempting, but I think it's going to be more than just passing the statement handle.

It's doubtful that this will work, but give it a try.

sub display
{

my $sth2 = shift;

print "Content-type:text/html\n\n";
......
......
......
while (@fields = $sth2->fetchrow())

With luck, one of these forms of calling the sub will work.

display(*$sth2);
display(\$sth2);
display($sth2);

If that doesn't work, you may need to pass the database handle and do the prepare and execute in the module.

edit:
You're also going to need to load the DBI module in your DisplayTable module.

hyperbole
03-06-2006, 11:32 PM
The method netroact is using should work as long as you initialize sth2 in the program that calls display, however, I think FishMonger's idea of including DBI in this module and opening the database in the module is probably the better way to go.


use DBI;

...
my $sth2;
sub display($)
{
...

if (not defined($sth2))
{ sth2 = DBI->connect(...); }

...
}

.

FishMonger
03-07-2006, 12:08 AM
Food for thought.

This is not a good example of the usage a module. A module should have a more generic usage that can be used in a variety of scripts rather than outputing your html page for a single script. There are better methods to include an external file. One of the better methods would be to use HTML::Template. Or, you could include the file via a do or require statement.

do "stock_table.pl";

netroact
03-07-2006, 02:09 AM
Yip! That's why I was wanting to use one module for all my scripts to use, so I wouldn't need to change all the scripts everytime I change the way the table displays. I think the HTML::Template module is probably the way I will go.

Thanks for the help!