in reply to Re^2: How could I check if a variable is 'read-only'?
in thread How could I check if a variable is 'read-only'?

Scalar::Util did not work for me

I would expect Scalar::Util to perform the task flawlessly.
Could you provide an example script that demonstrates its failing ?

Cheers,
Rob
  • Comment on Re^3: How could I check if a variable is 'read-only'?

Replies are listed 'Best First'.
Re^4: How could I check if a variable is 'read-only'?
by kschwab (Vicar) on Jan 02, 2019 at 14:39 UTC

    There does look like one possibility. Perlguts mentions that with Perl 5.16 and earlier: Copy-on-write (see the next section) shared a flag bit with read-only scalars. So the only way to test whether sv_setsv , etc., will raise a "Modification of a read-only value" error in those versions is: SvREADONLY(sv) && !SvIsCOW(sv)

    Scalar::Util doesn't do anything special for 5.16 or earlier, so it could have false positives in that case. Of course, 5.18 (where it's fixed) was released in 2013 :)

Re^4: How could I check if a variable is 'read-only'?
by Anonymous Monk on Jun 14, 2019 at 23:50 UTC

    What do you make of this?

    #!/usr/bin/env perl use strict; use warnings; use JSON; use Scalar::Util qw/ readonly /; my $data = decode_json('{ "val":false }'); print "readonly: ", readonly($data->{val}) ? 'yes' : 'no', "\n"; bless $data->{val};

    That produces this for me (on Perl 5.18.2):

    $./try readonly: no Modification of a read-only value attempted at ./try line 10.
      cant reproduce that here, try upgrading perl and or json module

        Or cite yours…?

        readonly: no Modification of a read-only value attempted at - line 9. # Successfully installed JSON-4.02 (upgraded from 2.97001) readonly: no Modification of a read-only value attempted at - line 8. # Scalar::Util is up to date. (1.50) perl -MScalar::Util=readonly -MJSON -E 'say readonly(JSON::false) ? "r +eadonly" : "ReAdOnLY"' ReAdOnLY # This is perl 5, version 18, subversion 4 (v5.18.4)