×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Modification of a read-only value attempted
2

Modification of a read-only value attempted

Modification of a read-only value attempted

(OP)
I wrote a small piece of codes to test out ReadOnly.pm.

CODE

use Readonly;

Readonly::Hash my %readOnlyHash1 => (
  'key11'=>{
    'key21'=>1,
    'key22'=>1,
  },
  'xyz'=>{
    'abc'=>1,
  },
);

Readonly::Hash my %readOnlyHash2 => (
  'xyz'=>{
    'abc'=>1,
  },
);

my $k1 = 'key11';
my $k2 = 'key31';
if(defined($readOnlyHash1{$k1}{$k2})) { # line 24
  print "Defined: Key 1 - $k1, Key 2 - $k2\n";
}
else {
  print "Not Defined: Key 1 - $k1, Key 2 - $k2\n";
}

if(defined($readOnlyHash2{$k1}{$k2})) { # line 31
  print "Defined: Key 1 - $k1, Key 2 - $k2\n";
}
else {
  print "Not Defined: Key 1 - $k1, Key 2 - $k2\n";
} 

And below is the output:

CODE

% test.pl
Not Defined: Key 1 - key11, Key 2 - key31
Modification of a read-only value attempted at ./test.pl line 31 

Question:
1) Why do I get this "Modification of a read-only value attempted" error?
2) Why doesn't the same error show at Line 24?

I don't see any difference between line 24 and line 31.

Thanks for the help.

RE: Modification of a read-only value attempted

Hi

Good questions. No answers from me.

My guess is, a dereferencing failure is misinterpreted in that module.
  • Line 28 would also throw error in case you would try to check an item of the undefined $readOnlyHash1{$k1}{$k2}, like $readOnlyHash1{$k1}{$k2}{huh}.
  • Line 31 would not crash either if you would use the -> operator, like $readOnlyHash2->{$k1}->{$k2}.
This is what makes me believe in fact is a dereferencing error in the background. ( Which belief not necessary has anything to do with the reality… )

By the way, the manual says

Quote (man perlfunc)

Use of defined on aggregates (hashes and arrays) is deprecated.
But using exists instead changes nothing in this case.

Feherke.
feherke.github.io

RE: Modification of a read-only value attempted

(OP)
Thank you Feherke for your input and for your reminding me using exists instead of defined.
Sorry for this delayed follow-up.

RE: Modification of a read-only value attempted

(OP)
Hi Feherke,

I retested the code using hash ref as you suggested. I got the same error.

Below is my code - 2 red lines are new.

CODE

#!/usr/bin/perl -w

use strict;
use Readonly;

Readonly::Hash my %readOnlyHash1_v => (
  'key11'=>{
    'key21'=>1,
    'key22'=>1,
  },
  'xyz'=>{
    'abc'=>1,
  },
);
Readonly::Scalar my $readOnlyHash1 => \%readOnlyHash1_v;

Readonly::Hash my %readOnlyHash2_v => (
  'xyz'=>{
    'abc'=>1,
  },
);
[/color][/b]Readonly::Scalar my $readOnlyHash2 => \%readOnlyHash2_v;[/color][/b]

my $k1 = 'key11';
my $k2 = 'key31';
if(exists($readOnlyHash1->{$k1}->{$k2})) { # line 26
  print "exists: Key 1 - $k1, Key 2 - $k2\n";
}
else {
  print "Not exists: Key 1 - $k1, Key 2 - $k2\n";
}

if(exists($readOnlyHash2->{$k1}->{$k2})) { # line 33
  print "exists: Key 1 - $k1, Key 2 - $k2\n";
}
else {
  print "Not exists: Key 1 - $k1, Key 2 - $k2\n";
} 

And the output:

CODE

% ./test.pl
Not exists: Key 1 - key11, Key 2 - key31
Modification of a read-only value attempted at ./test.pl line 33 

Thank you for your time and help.

RE: Modification of a read-only value attempted

In the second hash, $k1 does not exist, but, to test the existence of $readOnlyHash2->{$k1}->{$k2}, $readOnlyHash2->{$k1} must be created, whence the error. You should write:

CODE -->

if(exists($readOnlyHash2->{$k1}&&exists($readOnlyHash2->{$k1}->{$k2})) { 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: Modification of a read-only value attempted

(OP)
I see! Thank you, prex!

I thought if(exists($readOnlyHash2->{$k1}->{$k2})) would automatically test if(exists($readOnlyHash2->{$k1})) first.

Thanks again.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close