开发者

How does func2 know the context?

func1(func2);

As we know Perl needs to know the context to evaluate,but in the above case 开发者_JAVA技巧how does func2 know it's in scalar or list context?


If func1 does not have a prototype (or a @ prototype), it will be list context. If func1 has a prototype of $, then it will be scalar context.

Caveat: please do not use prototypes, they're evil.


Apparently. it's list.

$ perl
sub f1 { print "called f1\n" } 
sub f2 { print "called f2\n"; print wantarray ? "list": "scalar"; print "\n"; }
f1(f2);
^d
called f2
list
called f1

Why? That's another question entirely - I assume because function params are implicitly lists come what may.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜