chandantul has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I am getting the following error " Modification of a read-only value attempted" while setting variable for the JSON response, There are more than 1000 users entries and my code is getting stuck after 324 user name entries collection. Do you think there is some issue in JSON response of 325 user entry causing this issue? How i can avoid this error and proceed the code post 325 user entry and complete the code?

my $respons = $response[$i][$j]{creden}{uName};

Replies are listed 'Best First'.
Re: Modification of a read-only value attempted
by kcott (Archbishop) on Feb 02, 2021 at 05:41 UTC

    G'day chandantul,

    You should check perldiag when you encounter an error message that you don't understand. In this instance, "Modification of a read-only value attempted" describes a number of situations in which this particular message may be emitted.

    If that information doesn't help, you should provide us with an SSCCE that reproduces your problem. In many cases, the process of writing an SSCCE will identify the problem for you. A single line of code, with no context, does not help us to help you.

    Please also read: "How do I post a question effectively?".

    — Ken

Re: Modification of a read-only value attempted
by LanX (Saint) on Feb 01, 2021 at 14:43 UTC
    >   my $respons = $response[$i][$j]{creden}{uName};

    That's supposed to be the line causing this error?

    No way...

    Help us to help you by providing more insight.

    update

    the only possibility I see is auto-vivification of non existent entry "creden" provided $response[$i][$j] is read-only. (if that's even possible)

    A prior check would solve this!

    update

    though the error messages don't match.

    DB<72> $a{1}=\1 DB<73> use warnings; say $a{1}{foo}{bar} Not a HASH reference at (eval 81)[c:/Perl_524/lib/perl5db.pl:737] line + 2.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      So I was able to reproduce it
      DB<75> use Readonly; DB<77> Readonly::Hash %has => (key1 => 1, key2 => 2 ); DB<78> x \%has 0 HASH(0x332b388) 'key1' => 1 'key2' => 2 DB<79> $a{1}=\%has DB<80> use warnings; say $a{1}{foo}{bar} Modification of a read-only value attempted at (eval 94)[c:/Perl_524/l +ib/perl5db.pl:737] line 2. DB<81>

      you must check if there is any key {creden} before trying to access a deeper key.

      no autovivification is another possibility.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      This is the JSON response, i suppose to capture below for all 1000 users

      "creden": { "uName": "5xxxxxxx" },