FAQ
Shaji

thank you.
But the solution will not suffice.
As I said, there are various other functions in different packages which
are modifying an environment variable.
I want to have a lock or make it ready only so that only one can modify and
the rest cant.
This code will work but I have to make the same code change in all the
places where this hash value is being set.
so, this will not work for me.

Please tell me if there are other ways to handle this.

thanks,
Satya


On 12 May 2014 15:01, Shaji Kalidasan wrote:

Dear Satya,

Here is one way to do it.

[code]
use strict;
use warnings;
use Hash::Util qw (lock_keys lock_value);
use Data::Dumper;

my %hash=( name => "satya", number => "21");
my $ref=\%hash;
print ("\nBefore ".Dumper(%hash));

eval {
no warnings;
lock_value(%hash,"name");
$hash{name}="new name";
};

if ($@) {
print "An error occured : $@\n";
}

print ("\nAfter " . Dumper(%hash));
[/code]

[output]

Before $VAR1 = 'name';
$VAR2 = 'satya';
$VAR3 = 'number';
$VAR4 = '21';
An error occured : Modification of a read-only value attempted at
C:/Users/Shaji/workspace/Perl/sandbox/lock-hash-keys.pl line 13.


After $VAR1 = 'name';
$VAR2 = 'satya';
$VAR3 = 'number';
$VAR4 = '21';
[/output]

On Mon, May 12, 2014 at 2:29 PM, Satya Prasad Nemana wrote:

Hi

I have requirement to make the key of a perl hash to be read only so that
no other function can modify it.

I got Hash::Util package and managed to do what I needed

use strict;
use warnings;
use Hash::Util qw (lock_keys lock_value);
use Data::Dumper;
my %hash=( name => "satya", number => "21");
my $ref=\%hash;
print ("\nBefore ".Dumper(%hash));
lock_value(%hash,"name");
$hash{name}="new name";
print ("\nAfter ".Dumper(%hash));

The problem is that now I get the error

snemana@bats4:~/perlprogs>$perl lock_hash_keys.pl

Before $VAR1 = 'number';
$VAR2 = '21';
$VAR3 = 'name';
$VAR4 = 'satya';
Cannot usefully lock values in an unlocked hash at lock_hash_keys.plline 10
Modification of a read-only value attempted at lock_hash_keys.pl line 11.

As seen above the program halts when the attempt is made to assign value
to the hash key.
What I wanted is way to stop the assignment and throw and error but not
halt the program, but continue with the program.

Could someone help me with the error handling please?

Thanks,
Satya


--
best,
Shaji

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to
God.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


--
Satya Prasad

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 7 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedMay 12, '14 at 8:59a
activeMay 13, '14 at 3:51p
posts7
users5
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase