⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch4.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
print &quot;\n Re: $subject&quot;;<BR>

55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n\n&quot;;<BR>

56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n====================================================\n&quot;;

<BR>

57 }<BR>

58 sub Letter::ClaimMoney()&nbsp;&nbsp;{<BR>

59&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n You owe me money. Get your act together&quot;;

<BR>

60&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n Do you want me to send Bruno over to &quot;;<BR>

61&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n collect it , or are you gonna pay up?&quot;;<BR>

62 }<BR>

63 <BR>

64 sub Letter::ClaimMoneyNice()&nbsp;&nbsp;{<BR>

65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n It is come to my attention that your account is

&quot;;<BR>

66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n way over due.&quot;;<BR>

67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n You gonna pay us soon..&quot;;<BR>

68&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n or would you like me to come ovah?&quot;;<BR>

69 }<BR>

70 <BR>

71 sub Letter::ThreatBreakLeg() {<BR>

72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n apparently letters like these dont help&quot;;

<BR>

73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n I will have to make an example of you&quot;;<BR>

74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n \n See you in the hospital, pal!&quot;;<BR>

75 }<BR>

76 <BR>

77 sub Letter::ThankDem() {<BR>

78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

print &quot;\n\n Thanks for your support&quot;;<BR>

79 }<BR>

80 <BR>

81 sub Letter::Finish(){<BR>

82&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;\n\n\n\n Sincerely&quot;;<BR>

83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

printf &quot;\n Rudious \n &quot;;<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>,

&quot;Documenting Perl Scripts.&quot; 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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

local($name) = shift;<BR>

3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

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>, &quot;References.&quot;)

<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">&nbsp;1 #!/usr/bin/perl -w<BR>

&nbsp;2 <BR>

&nbsp;3 push(@Inc,'pwd');

<BR>

&nbsp;4 use Finance;<BR>

&nbsp;5 <BR>

&nbsp;6 $loan = 5000.00;<BR>

&nbsp;7 $apr =&nbsp;&nbsp;3.5;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;APR

<BR>

&nbsp;8 $year = 10;&nbsp;&nbsp;&nbsp;#&nbsp;&nbsp;in years.<BR>

&nbsp;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 &quot;\n If interest is applied at end of year&quot;;

<BR>

17 print &quot;\n The future value for a loan of \$&quot; . $loan

. &quot;\n&quot;;<BR>

18 print &quot; at an APR of &quot;, $apr , &quot; for &quot;,&nbsp;&nbsp;$time,

&quot; years&quot;;<BR>

19 printf &quot; is %8.2f \n&quot; , $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;&nbsp;&nbsp;&nbsp;# APR<BR>

26 $time = $year * 12; # in months<BR>

27 $fv2 = Finance::FutureValue($loan,$rate,$time);<BR>

28 <BR>

29 print &quot;\n If interest is applied at end of each month&quot;;

<BR>

30 print &quot;\n The future value for a loan of \$&quot; . $loan

. &quot;\n&quot;;<BR>

31 print &quot; at an APR of &quot;, $apr , &quot; for &quot;,&nbsp;&nbsp;$time,

&quot; months&quot;;<BR>

32 printf &quot; is %8.2f \n&quot; , $fv2;<BR>

33 <BR>

34 printf &quot;\n The difference in value is %8.2f&quot;, $fv2

- $fv1;<BR>

35 printf &quot;\n Therefore by applying interest at shorter time

periods&quot;;<BR>

36 printf &quot;\n we are actually getting more money in interest.\n&quot;;</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>&nbsp;If interest is

applied at end of year<BR>

&nbsp;The future value for a loan of $5000<BR>

&nbsp;at an APR of 3.5 for

10 years is&nbsp;&nbsp;7052.99<BR>

<BR>

&nbsp;If interest is applied

at end of each month<BR>

&nbsp;The future value for a loan of $5000<BR>

&nbsp;at an APR of 3.5 for

120 months is&nbsp;&nbsp;7091.72<BR>

<BR>

&nbsp;The difference in value

is&nbsp;&nbsp;&nbsp;&nbsp;38.73<BR>

&nbsp;Therefore by applying interest at shorter time periods<BR>

&nbsp;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">&nbsp;&nbsp;1 package Finance;<BR>

&nbsp;&nbsp;2 <BR>

&nbsp;&nbsp;3 require Exporter;

<BR>

&nbsp;&nbsp;4 @ISA = (Exporter);<BR>

&nbsp;&nbsp;5 <BR>

&nbsp;&nbsp;6 =head1 Finance.pm<BR>

&nbsp;&nbsp;7 <BR>

&nbsp;&nbsp;8 Financial Calculator - Financial calculations made

easy with Perl<BR>

&nbsp;&nbsp;9 <BR>

&nbsp;10 =head 2<BR>

&nbsp;11 use Finance;<BR>

&nbsp;12 <BR>

&nbsp;13 $pv = 10000.0;<BR>

&nbsp;14 <BR>

&nbsp;15 $rate = 12.5 / 12;

# APR per month.<BR>

&nbsp;16 <BR>

&nbsp;17 $time = 360 ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

months for loan to mature<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -