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

📄 ch27.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
file (Listing 27.3) holds the C routines that contain all the

C code for the extension, and the <TT><FONT FACE="Courier">Finance.pm</FONT></TT>

file (Listing 27.2) contains routines that tell Perl how to load

this extension and what functions are exported.

<H3><A NAME="Step3CreateaMakefile"><B>Step 3: Create a </B><TT><B><FONT SIZE=4 FACE="Courier">Makefile</FONT></B></TT></A>

</H3>

<P>

In Step 3, you have to generate a <TT><FONT FACE="Courier">makefile</FONT></TT>.

Generating and invoking the <TT><FONT FACE="Courier">make</FONT></TT>

command <TT><FONT FACE="Courier">makefile</FONT></TT> will create

a working version of the library <TT><FONT FACE="Courier">Finance.so</FONT></TT>

in the <TT><FONT FACE="Courier">../../lib/ext/Finance</FONT></TT>

directory. After testing, you can move the finished versions of

these files to the <TT><FONT FACE="Courier">/usr/lib</FONT></TT>

or <TT><FONT FACE="Courier">/usr/local/lib</FONT></TT> tree. In

all further testing in this section, you must point the <TT><FONT FACE="Courier">@Inc</FONT></TT>

array to the <TT><FONT FACE="Courier">../../lib/ext/Finance</FONT></TT>

location for this <TT><FONT FACE="Courier">finance.so</FONT></TT>

file.

<P>

You may also see a <TT><FONT FACE="Courier">blib</FONT></TT> directory

in the <TT><FONT FACE="Courier">ext/Finance</FONT></TT> directory.

The <TT><FONT FACE="Courier">man</FONT></TT> page templates for

your extensions are kept here.

<P>

Finally, the <TT><FONT FACE="Courier">Finance.xs</FONT></TT> file

where you place all your code for extension in C is shown in Listing

27.3.

<HR>

<BLOCKQUOTE>

<B>Listing 27.3. The initial </B><TT><B><FONT FACE="Courier">Finance.xs</FONT></B></TT><B>

file.<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #ifdef _&nbsp;_cplusplus<BR>

&nbsp;2 extern &quot;C&quot; {<BR>

&nbsp;3 #endif<BR>

&nbsp;4 #include &quot;EXTERN.h&quot;<BR>

&nbsp;5 #include &quot;perl.h&quot;<BR>

&nbsp;6 #include &quot;XSUB.h&quot;<BR>

&nbsp;7 #ifdef _&nbsp;_cplusplus<BR>

&nbsp;8 }<BR>

&nbsp;9 #endif<BR>

10<BR>

11 static int<BR>

12 not_here(s)<BR>

13 char *s;<BR>

14 {<BR>

15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;croak(&quot;%s not implemented

on this architecture&quot;, s);<BR>

16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<BR>

17 }<BR>

18<BR>

19 static double<BR>

20 constant(name, arg)<BR>

21 char *name;<BR>

22 int arg;<BR>

23 {<BR>

24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = 0;<BR>

25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (*name) {<BR>

26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = EINVAL;<BR>

28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>

29<BR>

30 not_there:<BR>

31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = ENOENT;<BR>

32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>

33&nbsp;}<BR>

34<BR>

35 MODULE = Finance&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PACKAGE

= Finance<BR>

36<BR>

37 double<BR>

38 constant(name,arg)<BR>

39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name

<BR>

40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg</FONT></TT>

</BLOCKQUOTE>

<HR>

<H3><A NAME="Step4AddSomeCode"><B>Step 4: Add Some Code</B></A>

</H3>

<P>

Now that you have created the necessary files for your own Perl

extension, you can move to Step 3, which involves adding your

own code to the newly generated extension files. Add some simple

<TT><FONT FACE="Courier">futureValue</FONT></TT> world application

code to the <TT><FONT FACE="Courier">Finance.xs</FONT></TT> file.

Listing 27.4 shows what <TT><FONT FACE="Courier">Finance.xs</FONT></TT>

looks like with the code addition. Be sure to create this file

because you'll be using it throughout the rest of the chapter.

<HR>

<BLOCKQUOTE>

<B>Listing 27.4. After code is added to </B><TT><B><FONT FACE="Courier">Finance.xs</FONT></B></TT><B>.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;&nbsp;1 #ifdef _&nbsp;_cplusplus

<BR>

&nbsp;&nbsp;2 extern &quot;C&quot; {<BR>

&nbsp;&nbsp;3 #endif<BR>

&nbsp;&nbsp;4 #include &quot;EXTERN.h&quot;<BR>

&nbsp;&nbsp;5 #include &quot;perl.h&quot;<BR>

&nbsp;&nbsp;6 #include &quot;XSUB.h&quot;<BR>

&nbsp;&nbsp;7 #ifdef _&nbsp;_cplusplus<BR>

&nbsp;&nbsp;8 }<BR>

&nbsp;&nbsp;9 #endif<BR>

&nbsp;10<BR>

&nbsp;11 static int<BR>

&nbsp;12 not_here(s)<BR>

&nbsp;13 char *s;<BR>

&nbsp;14 {<BR>

&nbsp;15&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;croak(&quot;%s not implemented

on this architecture&quot;, s);<BR>

&nbsp;16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;<BR>

&nbsp;17 }<BR>

&nbsp;18<BR>

&nbsp;19 static double<BR>

&nbsp;20 constant(name, arg)<BR>

&nbsp;21 char *name;<BR>

&nbsp;22 int arg;<BR>

&nbsp;23 {<BR>

&nbsp;24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = 0;<BR>

&nbsp;25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (*name) {<BR>

&nbsp;26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>

&nbsp;27&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = EINVAL;<BR>

&nbsp;28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>

&nbsp;29<BR>

&nbsp;30 not_there:<BR>

&nbsp;31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno = ENOENT;<BR>

&nbsp;32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;<BR>

&nbsp;33&nbsp;}<BR>

&nbsp;34<BR>

&nbsp;35 MODULE = Finance&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PACKAGE

= Finance<BR>

&nbsp;36<BR>

&nbsp;37 double<BR>

&nbsp;38 constant(name,arg)<BR>

&nbsp;39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name

<BR>

&nbsp;40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arg

<BR>

&nbsp;41<BR>

&nbsp;42 double<BR>

&nbsp;43 futureValue(present,rate,time)<BR>

&nbsp;44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double present<BR>

&nbsp;45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double rate<BR>

&nbsp;46&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double time<BR>

&nbsp;47<BR>

&nbsp;48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CODE:<BR>

&nbsp;49&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double d;<BR>

&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extern double pow(double

x, double y);<BR>

&nbsp;51<BR>

&nbsp;52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = present * pow((1.0

+ rate),time);<BR>

&nbsp;53&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* printf(&quot;\n

Future Value = %f \n&quot;, d); */<BR>

&nbsp;54&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETVAL =&nbsp;&nbsp;d;

<BR>

&nbsp;55<BR>

&nbsp;56&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OUTPUT:<BR>

&nbsp;57&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETVAL<BR>

&nbsp;58<BR>

&nbsp;59 double<BR>

&nbsp;60 presentValue(future,rate,time)<BR>

&nbsp;61&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double future<BR>

&nbsp;62&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double rate<BR>

&nbsp;63&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double time<BR>

&nbsp;64<BR>

&nbsp;65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CODE:<BR>

&nbsp;66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double d;<BR>

&nbsp;67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extern double pow(double

x, double y);<BR>

&nbsp;68<BR>

&nbsp;69&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d = future / pow((1.0

+ rate),time);<BR>

&nbsp;70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* printf(&quot;\n

Present Value = %f \n&quot;, d); */<BR>

&nbsp;71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETVAL =&nbsp;&nbsp;d;

<BR>

&nbsp;72<BR>

&nbsp;73&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OUTPUT:<BR>

&nbsp;74&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETVAL<BR>

&nbsp;75<BR>

&nbsp;76 void<BR>

&nbsp;77 Gordon(d,r,g)<BR>

&nbsp;78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double d<BR>

&nbsp;79&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double r<BR>

&nbsp;80&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double g<BR>

&nbsp;81<BR>

&nbsp;82&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CODE:<BR>

&nbsp;83&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*

Return Gordon Growth Model Value of stock */<BR>

&nbsp;84&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g = d / (r -g);<BR>

&nbsp;85&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;\n g

= %f&quot;, g);<BR>

&nbsp;86<BR>

&nbsp;87 void<BR>

&nbsp;88 depreciateSL(p,s,n)<BR>

&nbsp;89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double p<BR>

&nbsp;90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double s<BR>

&nbsp;91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double n<BR>

&nbsp;92<BR>

&nbsp;93&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PpcODE:<BR>

&nbsp;94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double sum;<BR>

&nbsp;95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double value[10];

<BR>

&nbsp;96&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double dep;<BR>

&nbsp;97&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int i;<BR>

&nbsp;98&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int in;<BR>

&nbsp;99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;&nbsp;= (int)n;

<BR>

100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((n &lt; 10) &amp;&amp;

(n &gt; 0))<BR>

101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EXTEND(sp,in);

<BR>

103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;&nbsp;=

p;<BR>

104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;value

= %f for %d years \n&quot;, sum,in);<BR>

105&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dep

= (sum - s)/ n;<BR>

106&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for

(i=0; i&lt;n; i++)<BR>

107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

<BR>

108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum

-= dep;<BR>

109&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;value

= %f \n&quot;, sum);<BR>

110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value[i]

= sum;<BR>

111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PUSHs(sv_2mortal(newSViv(sum)));

<BR>

112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

<BR>

113&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

A function to calculate the future value of an investment is defined

in line 43. The function to calculate the present value of money

to be received in the future from an investment is defined starting

at line 60. Both functions return one value and require three

arguments, which are defined one per each line following the function

declaration. For example, for the present value function in line

60, the three arguments must be defined in one line as

<BLOCKQUOTE>

<TT><FONT FACE="Courier">double future, rate, time;</FONT></TT>

</BLOCKQUOTE>

<P>

At line 77, I define a Gordon Growth model function for a stock.

This function only prints something and does not return any values.

At line 88, I define a straight line depreciation model function

that returns more than one value on the calling stack.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD><B>Caution</B></TD></TR>

<TR VALIGN=TOP><TD>

<BLOCKQUOTE>

Be careful to put the type of function and the name of the function on separate lines. In other words, do not shorten the two lines into one. The return parameter and the function name must be on two lines for the XS specification. Any arguments to the 

function would have be to listed one at a time on each line following the function name.</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

Next, run the command <TT><FONT FACE="Courier">perl Makefile.PL</FONT></TT>.

This creates a real makefile, which <TT><FONT FACE="Courier">make</FONT></TT>

needs. The <TT><FONT FACE="Courier">Makefile.PL</FONT></TT> checks

to see whether your Perl distribution is complete and then writes

the makefile for you.

<P>

If you get any errors at this point, you should check to see whether

your Perl distribution is complete. As a check, try running the

<TT><FONT FACE="Courier">Makefile.PL</FONT></TT> script as <TT><FONT FACE="Courier">root</FONT></TT>.

⌨️ 快捷键说明

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