readonly
readonly copied to clipboard
Refs-to-refs not handled correctly (or at least as I expected)
[Edit: See next comment for a related problem where refs-to-refs aren't readonly]
If the thing being Readonly::Cloned is a ref-to-scalar, you get back the scalar, without the reference:
#!/usr/bin/perl
use strict; use warnings; use feature 'say';
use Data::Dumper;
use Readonly ();
my $foo = 42;
my $original = \$foo;
my $copy = Readonly::Clone($original);
say Data::Dumper->new([$original,$copy],["original","copy"])->Dump;
RESULTS:
$original = \42;
$copy = 42;
I'm assuming Readonly::Clone() is intended to have the same API as Clone::clone().
Using Readonly 2.05 with Perl v5.34.0
There may be a more general bug with how references to other references are handled (i.e. not just at the top level). It seems like the refs themselves are not made read-only, nor anything they point to.
Here is an example of a "read only" structure which is actually mutuable:
#!/usr/bin/env perl
use strict; use warnings; use feature qw/say/;
use Data::Dumper; $Data::Dumper::Indent = 0;
use Readonly ();
Readonly::Scalar my $roitem => { foo => \\\{ bar => 42 } };
say Data::Dumper->new([$roitem],['roitem'])->Dump;
$$${$roitem->{foo}}->{bar} += 1000; # SHOULD DIE HERE?
say Data::Dumper->new([$roitem],['roitem'])->Dump;
${$roitem->{foo}} = "something else"; # SHOULD DIE HERE?
say Data::Dumper->new([$roitem],['roitem'])->Dump;
OUTPUT:
$roitem = {'foo' => \\\{'bar' => 42}};
$roitem = {'foo' => \\\{'bar' => 1042}};
$roitem = {'foo' => \'something else'};