免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3369 | 回复: 3
打印 上一主题 下一主题

perl不能像C那样传递参数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-25 11:20 |只看该作者 |倒序浏览
这样写不行吗?
sub display2 ($d1, $d2) {
        print "\n d1 is $d1, and d2 is $d2 \n";
}
display2 (3,4);

给我返回:
Malformed prototype for main::display2: $d1,$d2 at 1.pl line XX.

是不是不能用上面这种类似C的方式?谢谢。

论坛徽章:
0
2 [报告]
发表于 2008-01-25 12:15 |只看该作者
perl 的sub不需要显示定义参数。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2008-01-25 13:07 |只看该作者

论坛徽章:
0
4 [报告]
发表于 2008-01-25 14:49 |只看该作者
谢谢两位!
-------------------------------------------------------------
Here is an interesting idea that doesn't involve the Debug filter. Currently Perl subroutines have fairly limited support for formal parameter lists. You can specify the number of parameters and their type, but you still have to manually take them out of the @_ array yourself. Write a source filter that allows you to have a named parameter list. Such a filter would turn this:

    sub MySub ($first, $second, @rest) { ... }into this:

    sub MySub($$@) {
       my ($first) = shift;
       my ($second) = shift;
       my (@rest) = @_;
       ...
    }Finally, if you feel like a real challenge, have a go at writing a full-blown Perl macro preprocessor as a source filter. Borrow the useful features from the C preprocessor and any other macro processors you know. The tricky bit will be choosing how much knowledge of Perl's syntax you want your filter to have.

------------------------------------------------
就是说,perl总是用@_来中转参数,且不会自动放入子函数的参数中,需要手工处理,对不?
比较古怪。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP