Subject: | Can't create a constant array of CODE refs |
Date: | Mon, 25 Mar 2013 21:58:22 +0100 |
To: | bug-const-fast [...] rt.cpan.org |
From: | Victor ADAM <victor.adam [...] derpymail.org> |
I'm honestly surprised this went unnoticed for so long. Any attempt to
create a read-only array or hash containing code references fails loudly:
perl -MConst::Fast -e 'const @a => sub{}'
Can't store CODE items at
/home/grimy/perl/lib/site_perl/5.16.2/Const/Fast.pm line 28.
It turns out CODE refs can't (and don't need to) be cloned, so the fix is
to simpl add <code>&& reftype $_[0] ne 'CODE'</code> to the $needs_cloning
check. Contextual diff below.
--- Fast.pm 2012-09-07 23:50:33.000000000 +0200
+++ Fast.new 2013-03-25 21:41:42.960761305 +0100
@@ -24,7 +24,7 @@
sub _make_readonly {
my (undef, $dont_clone) = @_;
if (my $reftype = reftype $_[0] and not blessed($_[0]) and not
&Internals::SvREADONLY($_[0])) {
- my $needs_cloning = !$dont_clone && &Internals::SvREFCNT($_[0]) > 1;
+ my $needs_cloning = !$dont_clone && &Internals::SvREFCNT($_[0]) > 1
&& reftype $_[0] ne 'CODE';
$_[0] = _dclone($_[0]) if $needs_cloning;
&Internals::SvREADONLY($_[0], 1);
if ($reftype eq 'SCALAR' || $reftype eq 'REF') {