注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

操作系统、汇编和c语言历史

软件版本历史,操作系统史,开发工具史,编程语言历史

 
 
 
 
 

日志

 
 

then stores it in the stack (as the first element—element 0).  

2011-04-29 21:29:02|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
The XSRETURN_xxx() macros work by creating a mortal SV and putting it as the first element of the
stack. It then uses the XSRETURN(1) macro, indicating that Perl can expect one value on the stack.
Example 10.15 sets two return values on the stack and returns, informing Perl to expect two return
values. Notice that line 5 creates a new string-based SV, and line 6 first marks the SV as mortal and
then stores it in the stack (as the first element—element 0). Line 7 creates a new SV, which is a
floating-point number, marks it as mortal, and then stores it as the second element in the stack.
Example 10.15 Returning two values from a function
01. XS( MyFunction )
02. {
03. dXSARGS;
04. char *pszTest = "My test string!";
Create PDF with GO2PDF for free, if you wish to remove this line, click here to buy Virtual PDF Printer
05. SV* pSV = newSVpv( pszTest, strlen(pszTest) );
06. ST( 0 ) = sv_2mortal( pSV );
07. ST( 1 ) = sv_2mortal( newSVnv( (double) 6.02 ) );
08. XSRETURN( 2 );
09. }

Both of the returned SVs were marked as mortal so that they would die after the function returns
back to Perl. If the calling Perl scripts assign these return values to variables upon return of the
function, their reference counts are increased from 0 to 1. This will prevent them from being
automatically destroyed by Perl. If the script discards these values (does not assign the return values
to any variable), however, they will indeed be destroyed.
Example 10.16 demonstrates using the ST() macro to obtain values passed into the function from
the calling Perl script. Notice that line 4 checks the items variable (which was initialized with the
dXSARGS macro) to see whether two values were indeed passed in. Line 9 adds the integer values of
both passed-in SVs, and line 10 uses the XSRETURN_IV() macro to tell Perl that it is returning one
long value. If the two parameters are not passed into the function, line 14 tells Perl to return undef.
Example 10.16 Returning one value from a function
01. XS( MyFunction2 )
02. {
03. dXSARGS;
04. if( 2 == items )
05. {
06. long lSum;
07. SV* pSV1 = ST( 0 );
08. SV* pSV2 = ST( 1 );
09. lSum = SvIV( pSV1 ) + SvIV( pSV2 );
10. XSRETURN_IV( lSum );
11. }
12. else
13. {
14. XSRETURN_UNDEF;
15. }
16. }
The Perl script that calls this can expect two return values:
($Text, $AvagadrosNum) = Win32::Test::MyFunction();
Modifying Input Parameters
You might notice that some extensions change input variables. You might see a Perl script call
some function such as this,
  评论这张
 
阅读(148)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017