开发者

How do you reproduce "Can't use an undefined value as a symbol reference " in Perl?

开发者 https://www.devze.com 2023-03-21 09:43 出处:网络
Can\'t use an undefined value as a symbol referenc开发者_开发百科e Anyone knows how to reproduce it?print $bar 1;
Can't use an undefined value as a symbol referenc开发者_开发百科e 

Anyone knows how to reproduce it?


print $bar 1;

emits the error. you can see a nice explanation of what it means on this perlmonks thread

as explained by splain:

Can't use an undefined value as a symbol reference (#1)
(F) A value used as either a hard reference or a symbolic reference must
be a defined value.  This helps to delurk some insidious errors.


Here's the simplest form:

say *{my $a}{SCALAR}; # OR say ${ *{my $a} };


perl -we '$x = shift; *$x = sub {42}; print $x->()'

This code prints 42 if called with ANY argument (incl. ''), but gives the needed warning w/o args.

0

精彩评论

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

关注公众号