📄 ch4.htm
字号:
print "\n Re: $subject";<BR>
55
print "\n\n";<BR>
56
print "\n====================================================\n";
<BR>
57 }<BR>
58 sub Letter::ClaimMoney() {<BR>
59
print "\n You owe me money. Get your act together";
<BR>
60
print "\n Do you want me to send Bruno over to ";<BR>
61
print "\n collect it , or are you gonna pay up?";<BR>
62 }<BR>
63 <BR>
64 sub Letter::ClaimMoneyNice() {<BR>
65
print "\n It is come to my attention that your account is
";<BR>
66
print "\n way over due.";<BR>
67
print "\n You gonna pay us soon..";<BR>
68
print "\n or would you like me to come ovah?";<BR>
69 }<BR>
70 <BR>
71 sub Letter::ThreatBreakLeg() {<BR>
72
print "\n apparently letters like these dont help";
<BR>
73
print "\n I will have to make an example of you";<BR>
74
print "\n \n See you in the hospital, pal!";<BR>
75 }<BR>
76 <BR>
77 sub Letter::ThankDem() {<BR>
78
print "\n\n Thanks for your support";<BR>
79 }<BR>
80 <BR>
81 sub Letter::Finish(){<BR>
82
printf "\n\n\n\n Sincerely";<BR>
83
printf "\n Rudious \n ";<BR>
84 }<BR>
85 <BR>
86 1;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Lines containing the equal sign are used for documentation. You
must document each module for your own reference; Perl modules
do not need to be documented, but it's a good idea to write a
few lines about what your code does. A few years from now, you
may forget what a module is about. Good documentation is always
a must if you want to remember what you did in the past!
<P>
I cover documentation styles used for Perl in <A HREF="ch8.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch8.htm" >Chapter 8</A>,
"Documenting Perl Scripts." For this sample module,
the <TT><FONT FACE="Courier">=head1</FONT></TT> statement begins
the documentation. Everything up to the <TT><FONT FACE="Courier">=cut</FONT></TT>
statement is ignored by the Perl interpreter.
<P>
Next, the module lists all the functions exported by this module
in the <TT><FONT FACE="Courier">@EXPORT</FONT></TT> array. The
<TT><FONT FACE="Courier">@EXPORT</FONT></TT> array defines all
the function names that can be called by outside code. If you
do not list a function in this <TT><FONT FACE="Courier">@EXPORT</FONT></TT>
array, it won't be seen by external code modules.
<P>
Following the <TT><FONT FACE="Courier">@EXPORT</FONT></TT> array
is the body of the code, one subroutine at a time. After all the
subroutines are defined, the final statement <TT><FONT FACE="Courier">1;</FONT></TT>
ends the module file. <TT><FONT FACE="Courier">1;</FONT></TT>
must be the last executable line in the file.
<P>
Let's look at some of the functions defined in this module. The
first function to look at is the simple <TT><FONT FACE="Courier">Date</FONT></TT>
function, lines 43 to 46, which prints the current UNIX date and
time. There are no parameters to this function, and it doesn't
return anything meaningful back to the caller.
<P>
Note the use of <TT><FONT FACE="Courier">my</FONT></TT> before
the <TT><FONT FACE="Courier">$date</FONT></TT> variable in line
44. The <TT><FONT FACE="Courier">my</FONT></TT> keyword is used
to limit the scope of the variable to within the <TT><FONT FACE="Courier">Date</FONT></TT>
function's curly braces. Code between curly braces is referred
to as a <I>block</I>. Variables declared within a block are limited
in scope to within the curly braces. In 49 and 50, the local variables
<TT><FONT FACE="Courier">$name</FONT></TT> and <TT><FONT FACE="Courier">$subject</FONT></TT>
are visible to all functions.
<P>
You can also declare variables with the <TT><FONT FACE="Courier">local</FONT></TT>
qualifier. The use of <TT><FONT FACE="Courier">local</FONT></TT>
allows a variable to be in scope for the current block as well
as for other blocks of code called from within this block. Thus,
a local <TT><FONT FACE="Courier">$x</FONT></TT> declared within
one block is visible to all subsequent blocks called from within
this block and can be referenced. In the following sample code,
the <TT><FONT FACE="Courier">ToTitled</FONT></TT> function's <TT><FONT FACE="Courier">$name</FONT></TT>
variable can be accessed but not the data in <TT><FONT FACE="Courier">$iphone</FONT></TT>:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 sub Letter::ToTitled {<BR>
2
local($name) = shift;<BR>
3
my($phone) = shift;</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="SubroutinesandPassingParameters"><FONT SIZE=5 COLOR=#FF0000>Subroutines
and Passing Parameters</FONT></A></H2>
<P>
The sample code for <TT><FONT FACE="Courier">Letter.pm</FONT></TT>
showed how to extract one parameter at a time. The subroutine
<TT><FONT FACE="Courier">To()</FONT></TT> takes two parameters
to set up the header for the memo.
<P>
Using functions within a module is not any different than using
and defining Perl modules within the same code file. Parameters
are passed by reference unless otherwise specified. Multiple arrays
passed into a subroutine, if not explicitly dereferenced using
the backslash, are concatenated.
<P>
The <TT><FONT FACE="Courier">@_</FONT></TT> input array in a function
is always an array of scalar values. Passing values by reference
is the preferred way in Perl to pass a large amount of data into
a subroutine. (<A HREF="ch3.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch3.htm" >See Chapter 3</A>, "References.")
<H2><A NAME="AnotherSampleModuleFinance"><FONT SIZE=5 COLOR=#FF0000>Another
Sample Module: </FONT><TT><FONT SIZE=5 COLOR=#FF0000 FACE="Courier">Finance</FONT></TT></A>
</H2>
<P>
The <TT><FONT FACE="Courier">Finance</FONT></TT> module, shown
in Listing 4.3, is used to provide simple calculations for loan
values. Using the <TT><FONT FACE="Courier">Finance</FONT></TT>
module is straightforward. All the functions are written with
the same parameters, as shown in the formula for the functions.
<P>
Let's look at how the future value of an investment can be calculated.
For example, if you invest some dollars, <TT><FONT FACE="Courier">$pv</FONT></TT>,
in a bond that offers a fixed percentage rate, <TT><FONT FACE="Courier">$r</FONT></TT>,
applied at known intervals for <TT><FONT FACE="Courier">$n</FONT></TT>
time periods, what is the value of the bond at the time of its
expiration? In this case, you'll be using the following formula:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$fv = $pv * (1+$r) ** $n ;</FONT></TT>
</BLOCKQUOTE>
<P>
The function to get the future value is declared as <TT><FONT FACE="Courier">FutureValue</FONT></TT>.
Refer to Listing 4.3 to see how to use it.
<HR>
<BLOCKQUOTE>
<B>Listing 4.3. Using the </B><TT><B><FONT FACE="Courier">Finance</FONT></B></TT><B>
module.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl -w<BR>
2 <BR>
3 push(@Inc,'pwd');
<BR>
4 use Finance;<BR>
5 <BR>
6 $loan = 5000.00;<BR>
7 $apr = 3.5; # APR
<BR>
8 $year = 10; # in years.<BR>
9 <BR>
10 # ----------------------------------------------------------------
<BR>
11 # Calculate the value at the end of the loan if interest<BR>
12 # is applied every year.<BR>
13 # ----------------------------------------------------------------
<BR>
14 $time = $year;<BR>
15 $fv1 = Finance::FutureValue($loan,$apr,$time);<BR>
16 print "\n If interest is applied at end of year";
<BR>
17 print "\n The future value for a loan of \$" . $loan
. "\n";<BR>
18 print " at an APR of ", $apr , " for ", $time,
" years";<BR>
19 printf " is %8.2f \n" , $fv1;<BR>
20 <BR>
21 # ----------------------------------------------------------------
<BR>
22 # Calculate the value at the end of the loan if interest<BR>
23 # is applied every month.<BR>
24 # ----------------------------------------------------------------
<BR>
25 $rate = $apr / 12; # APR<BR>
26 $time = $year * 12; # in months<BR>
27 $fv2 = Finance::FutureValue($loan,$rate,$time);<BR>
28 <BR>
29 print "\n If interest is applied at end of each month";
<BR>
30 print "\n The future value for a loan of \$" . $loan
. "\n";<BR>
31 print " at an APR of ", $apr , " for ", $time,
" months";<BR>
32 printf " is %8.2f \n" , $fv2;<BR>
33 <BR>
34 printf "\n The difference in value is %8.2f", $fv2
- $fv1;<BR>
35 printf "\n Therefore by applying interest at shorter time
periods";<BR>
36 printf "\n we are actually getting more money in interest.\n";</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Here is sample input and output of Listing 4.3.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$ <B>testme<BR>
<BR>
</B> If interest is
applied at end of year<BR>
The future value for a loan of $5000<BR>
at an APR of 3.5 for
10 years is 7052.99<BR>
<BR>
If interest is applied
at end of each month<BR>
The future value for a loan of $5000<BR>
at an APR of 3.5 for
120 months is 7091.72<BR>
<BR>
The difference in value
is 38.73<BR>
Therefore by applying interest at shorter time periods<BR>
we are actually getting more money in interest.</FONT></TT>
</BLOCKQUOTE>
<P>
The revelation in the output is the result of the comparison of
values between <TT><FONT FACE="Courier">$fv1</FONT></TT> and <TT><FONT FACE="Courier">$fv2</FONT></TT>.
The <TT><FONT FACE="Courier">$fv1</FONT></TT> value is calculated
with the application of interest once every year over the life
of the bond. <TT><FONT FACE="Courier">$fv2</FONT></TT> is the
value if the interest is applied every month at the equivalent
monthly interest rate.
<P>
The <TT><FONT FACE="Courier">Finance.pm</FONT></TT> package is
shown in Listing 4.4 in its early development stages.
<HR>
<BLOCKQUOTE>
<B>Listing 4.4. The </B><TT><B><FONT FACE="Courier">Finance.pm</FONT></B></TT><B>
package.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 package Finance;<BR>
2 <BR>
3 require Exporter;
<BR>
4 @ISA = (Exporter);<BR>
5 <BR>
6 =head1 Finance.pm<BR>
7 <BR>
8 Financial Calculator - Financial calculations made
easy with Perl<BR>
9 <BR>
10 =head 2<BR>
11 use Finance;<BR>
12 <BR>
13 $pv = 10000.0;<BR>
14 <BR>
15 $rate = 12.5 / 12;
# APR per month.<BR>
16 <BR>
17 $time = 360 ; #
months for loan to mature<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -