Subject: | copy magic on subs puts raw CV in $_[3] |
% cat wtf.pl
#!perl
use strict;
use warnings;
use Variable::Magic qw(wizard cast);
my $wizard; BEGIN {
$wizard = wizard
copy => sub {
print "\$_[3] is $_[3]\n";
print "\\\$_[3] is " . \$_[3] . "\n";
print "\\&{\$_[3]} is " . \&{$_[3]} . "\n";
},
;
}
sub MODIFY_CODE_ATTRIBUTES {
my ($pkg, $sub) = @_;
cast &$sub, $wizard;
return;
}
my $i;
my $f = sub : Hello { $i };
__END__
% perl wtf.pl
Use of uninitialized value $_[3] in concatenation (.) or string at wtf.pl line 10.
$_[3] is
\$_[3] is CODE(0x86320d8)
\&{$_[3]} is CODE(0x86320d8)
The documentation for copy says:
"This magic only applies to tied arrays and hashes, and fires when you try to access or change their elements."
This is clearly not the whole story because it also fires for subs ... which is a good thing because I need this functionality.
But $_[3] seems to contain a raw CV, which I'm pretty sure isn't supposed to happen (hence it appearing as undef to Perl code). Instead I would've expected $_[3] to be a reference to a CV.
(\&{$_[3]} seems to be good workaround because it's compatible with both.)