📄 ch18.htm
字号:
24 $i2 = Invest::Stock('symbol'
=> 'INTC'); <BR>
25 $i3 = Invest::Stock('symbol'
=> 'MSFT', 'shares' => '10'); <BR>
26<BR>
27 $port->Invest::AddItem($i1);
<BR>
28 $port->Invest::AddItem($i2);
<BR>
29 $port->Invest::AddItem($i3);
<BR>
30<BR>
31 $port->showPortfolio();
<BR>
32 $port->savePortfolio("myStocks");
<BR>
33<BR>
34 =head1 DESCRIPTION<BR>
35<BR>
36 This module provides a short example of generating a
letter for a<BR>
37 friendly neighborbood loan shark.<BR>
38<BR>
39 The code begins after the "cut" statement.
<BR>
40 =cut<BR>
41<BR>
42 @EXPORT = qw( new, AddItem, showPortfolio, savePortfolio,
<BR>
43 reportPortfolio,
<BR>
44 restorePortfolio,
PrintMe);<BR>
45<BR>
46 my %portfolio = {};<BR>
47 my $portIndex = 0;<BR>
48<BR>
49 sub Invest::new {<BR>
50 my
$this = shift;<BR>
51 my
$class = ref($this) || $this;<BR>
52 my
$self = {};<BR>
53 bless
$self, $class;<BR>
54 $portIndex = 0;<BR>
55 # printf "\n Start portfolio";
<BR>
56 return
$self;<BR>
57 }<BR>
58<BR>
59 sub Invest::AddItem {<BR>
60 my ($type,$stock) = @_;<BR>
61 $portfolio{$portIndex} = $stock;
<BR>
62 # print "\nAdded ". $stock->{'shares'}
. " shares of " . $stock->{'symbol'};<BR>
63 $portIndex++;<BR>
64 }<BR>
65<BR>
66 sub Invest::showPortfolio {<BR>
67 printf "\n Our Portfolio
is:";<BR>
68 my ($key, $i);<BR>
69 while (($key,$i) = each(%portfolio))
{<BR>
70 print
"\n ". $i->{'shares'} . " shares
of " . $i->{'symbol'};<BR>
71 }<BR>
72 print "\n";<BR>
73 }<BR>
74<BR>
75 sub Invest::reportPortfolio {<BR>
76 my $hdrfmt = $~;<BR>
77 my $topfmt = $^;<BR>
78 my $pageCt = $=;<BR>
79 my $lineCt = $-;<BR>
80 my $sym;<BR>
81 my $shr;<BR>
82 my ($key, $i);<BR>
83<BR>
84 $~ = "PORT_RPT";<BR>
85 $^ = "PORT_RPT_TOP";
<BR>
86<BR>
87 format PORT_RPT_TOP =<BR>
88<BR>
89 Report<BR>
90 STOCK SHARES<BR>
91 ===== ======<BR>
92 .<BR>
93<BR>
94 format PORT_RPT =<BR>
95 @<<<< @<<<<
<BR>
96 $sym, $shr<BR>
97 .<BR>
98 # note how the code is intermingled
with the format!<BR>
99 while (($key,$i) = each(%portfolio))
{<BR>
100 $shr
= $i->{'shares'};<BR>
101 $sym
= $i->{'symbol'};<BR>
102 write
;<BR>
103<BR>
104 }<BR>
105<BR>
106 $= = $pageCt;<BR>
107 $- = $lineCt;<BR>
108 $~ = $hdrfmt;<BR>
109 $^ = $topfmt;<BR>
110 }<BR>
111<BR>
112 sub PrintMe {<BR>
113 my $this = shift;<BR>
114 print "\n Class : $$this";
<BR>
115 }<BR>
116<BR>
117 sub savePortfolio {<BR>
118 my ($this, $filename) = @_;<BR>
119 my %dummy;
<BR>
120 my $a;<BR>
121 my ($key, $val);<BR>
122 dbmopen(%dummy,$filename,0666);<BR>
123 while (($key,$val) = each(%portfolio))
{<BR>
124 $a =
"$key:" . $val->{'type'} . ":" . $val->{'symbol'}
. ":" . $val->{'shares'};<BR>
125 print
"\n Writing $key $a";<BR>
126 $dummy{$key}
= $a;<BR>
127 print
"\n Writing $dummy{$key}";<BR>
128 }<BR>
129 dbmclose(%dummy);<BR>
130 }<BR>
131<BR>
132 sub restorePortfolio {<BR>
133 my ($this, $filename) = @_;<BR>
134 my %dummy;
<BR>
135 my ($key, $val);<BR>
136 my ($ndx,$sec,$sym,$shr);<BR>
137 my $a;<BR>
138 local $i1;<BR>
139 dbmopen(%dummy,$filename,0666);<BR>
140 while (($key,$val) = each(%dummy))
{<BR>
141 $a =
$dummy{$key};<BR>
142 ($ndx,$sec,$sym,$shr)
= split(':',$a);<BR>
143 # print
"Read back $ndx,$sec,$sym,$shr \n";<BR>
144 if ($sec
eq 'Fund')<BR>
145
{<BR>
146
$i1 = Invest::Fund::new('Invest::Fund',<BR>
147 'symbol'
=> "$sym", 'shares' => "$shr");<BR>
148
}<BR>
149 else
<BR>
150
{<BR>
151
$i1 = Invest::Stock::new('Invest::Stock',<BR>
152 'symbol'
=> "$sym", 'shares' =>"$shr");<BR>
153
}<BR>
154 $this->Invest::AddItem($i1);
<BR>
155 $this->Invest::showPortfolio;
<BR>
156 }<BR>
157 dbmclose(%dummy);<BR>
158 }<BR>
159<BR>
160 1;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Line 8 is where the <TT><FONT FACE="Courier">require</FONT></TT>
statement is used to implement the <TT><FONT FACE="Courier">AnyDBM_FILE</FONT></TT>
support. Lines 17 through 19 use other packages as well.
<H2><A NAME="WhatIstheDBIPackage"><FONT SIZE=5 COLOR=#FF0000>What
Is the DBI Package?</FONT></A></H2>
<P>
The database interface (DBI) package for Perl is the implementation
of the DBI Application Program Interface (API) specification written
by Tim Bunce (<TT><FONT FACE="Courier">Tim.Bunce@ig.co.uk</FONT></TT>).
The DBI package API is designed specifically for use with Perl.
The set of functions and variables in the DBI package provide
a consistent interface to the application using it. The strong
point of the DBI package API, in addition to its broad set of
available functions, is that it completely isolates the using
application from the internal implementation of the underlying
database.
<P>
The DBI specification exists at various sites in the CPAN archives,
but the latest version (v0.6) is not up to date. The best source
of information is to look in the source files for a DBI package
itself. The entire specification is good for getting an idea of
how everything is intended to work together. However, the interface
has changed considerably since the specification was released.
Check out the file <TT><FONT FACE="Courier">dbispec.v06</FONT></TT>
in compressed form at <TT><FONT FACE="Courier">ftp.demon.co.uk</FONT></TT>
in the <TT><FONT FACE="Courier">/pub/perl/db</FONT></TT> directory.
<P>
The DBI specification started out as DBperl back in 1992 as a
team effort from several Perl enthusiasts. Here are the initial
contributors to the specification for each type of database:<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=171>infoperl (Informix)</TD><TD WIDTH=398>Kurt Andersen (<TT><FONT FACE="Courier">kurt@hpsdid.sdd.hp.com</FONT></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=171>interperl (Interbase)</TD><TD WIDTH=398>Buzz Moschetti (<TT><FONT FACE="Courier">buzz@fsrg.bear.com</FONT></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=171>oraperl (Oracle)</TD><TD WIDTH=398>Kevin Stock (<TT><FONT FACE="Courier">kstock@encore.com</FONT></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=171>sybperl (Sybperl)</TD><TD WIDTH=398>Michael Peppler (<TT><FONT FACE="Courier">mpeppler@itf.ch</FONT></TT>)
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=171>sqlperl/ingperl (Ingres)</TD><TD WIDTH=398>Ted Mellon (<TT><FONT FACE="Courier">dbi-users@fugue.com</FONT></TT>) and Tim Bunce
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The original DBI specification was edited by Kurt Anderson. In
1994, Tim Bunce took over the editing and maintenance of the specification
in addition to the DBI and <TT><FONT FACE="Courier">DBD::Oracle</FONT></TT>
package development. The specification and related files are copyrighted
by Tim Bunce.
<P>
The original specification was edited by Kurt Anderson from the
discussions on the mailing list. In 1993, Tim Bunce took over
the editing and maintenance of the specification and in 1994 started
the development of the DBI and <TT><FONT FACE="Courier">DBD::Oracle</FONT></TT>
modules. The DBI specification and modules are copyrighted by
Tim Bunce but are freely available to all with the same terms
as Perl. (Tim is the technical director of the software systems
house, Paul Ingram Group in Surrey, England. Tim can be reached
at <TT><FONT FACE="Courier">Tim.Bunce@ig.co.uk</FONT></TT>, but
DBI related mail should be sent to the <TT><FONT FACE="Courier">dbi-users@fugue.com</FONT></TT>
mailing list.)
<P>
The DBI is not related to any one specific database because it
serves as an intermediary between a program and one or more <TT><FONT FACE="Courier">DBD::*</FONT></TT>
driver modules. <TT><FONT FACE="Courier">DBD::</FONT></TT> modules
are drivers written to support a specific database back-end. The
<TT><FONT FACE="Courier">DBI::</FONT></TT> module manages all
installed <TT><FONT FACE="Courier">DBD::</FONT></TT> drivers in
your system. You can load and use more than one <TT><FONT FACE="Courier">DBD::</FONT></TT>
module at the same time.
<P>
<TT><FONT FACE="Courier">DBD::</FONT></TT> modules are written
in such a way that they may be copied and customized to suit your
specific needs. For example, the <TT><FONT FACE="Courier">DBD::Oracle</FONT></TT>
module served as the starting point for Alligator Descartes, another
well-known pioneer in developing database interfaces for Perl,
to develop <TT><FONT FACE="Courier">DBD::</FONT></TT> modules
for other databases. He has written two copyrighted documents
on how to develop your own driver from <TT><FONT FACE="Courier">DBD::</FONT></TT>
modules. These documents are located on the Web site <TT><FONT FACE="Courier">www.hermetica.com</FONT></TT>
in the <TT><FONT FACE="Courier">technologia/DBI</FONT></TT> directory.
<H3><A NAME="AvailablePackages">Available Packages</A></H3>
<P>
Some of <TT><FONT FACE="Courier">DBI::</FONT></TT> packages available
on the Internet are listed here; you can get the latest versions
of these files from the Internet CPAN sites:
<UL>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -