Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refs-to-refs not handled correctly (or at least as I expected) #41

Open
jimav opened this issue May 3, 2023 · 1 comment
Open

Refs-to-refs not handled correctly (or at least as I expected) #41

jimav opened this issue May 3, 2023 · 1 comment

Comments

@jimav
Copy link

jimav commented May 3, 2023

[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

@jimav
Copy link
Author

jimav commented May 3, 2023

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'};

@jimav jimav changed the title Readonly::Clone corrupts top-level reference-to-scalar (looses the ref) Refs-to-refs not handled correctly (or at least as I expected) May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant