Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Hash::Util alternative for compound hashes?

Tags:

hash

perl

hashref

I have a compound hashref as follows

my $ch = {
    k1 => [ { k=>1 }, { m=>2 } ],
    k2 => [ { l=>90}, ... ],
};

Hash::Util::lock_hashref_recurse($ch) does not effectively lock these values..

@{$ch->{k1}}[0]->{k} = 'New value'; is allowed ! How do i lock such a hashref completely ?

like image 416
trinity Avatar asked Aug 05 '13 11:08

trinity


People also ask

What are the alternatives to hashtab?

Hashtab is supported as a Windows shell extension and a Mac Finder plugin' and is a popular app in the OS & Utilities category. There are more than 50 alternatives to Hashtab for a variety of platforms, including Windows, Linux, Mac, BSD and PortableApps.com.

What hash algorithms does hashtab support?

HashTab supports many hash algorithms such as MD5, SHA1, SHA2, RipeMD, HAVAL and Whirlpool. Hashtab is supported as a Windows shell extension and a Mac Finder plugin' and is a popular app in the OS & Utilities category.

What is the use of recursive Hasher?

RHash (Recursive Hasher) is a console utility for computing and verifying hash sums of files. It supports CRC32, MD4, MD5, SHA1, SHA256, SHA512, Tiger, DC++ TTH, BitTorrent BTIH, ED2K, AICH, GOST R 34.


3 Answers

According to the documentation:

This method only recurses into hashes that are referenced by another hash. Thus a Hash of Hashes (HoH) will all be restricted, but a Hash of Arrays of Hashes (HoAoH) will only have the top hash restricted.

And you have a Hash of Arrays of Hashes

like image 99
Miguel Prz Avatar answered Oct 22 '22 20:10

Miguel Prz


use strictures;
use Hash::Util qw(lock_hash);
use Data::Visitor::Callback qw();

my $ch = {
    k1 => [{k => 1}, {m => 2}],
    k2 => [{l => 90},],
};

Data::Visitor::Callback->new(
    hash => sub {
        lock_hash %{ $_ }; 
        return $_;
    }
)->visit($ch);

$ch->{k1}[0]{k} = 'New value';
__END__
Modification of a read-only value attempted at …
like image 44
daxim Avatar answered Oct 22 '22 20:10

daxim


Hash::Util itself provides you a low-level function that you can replicate in Perl without XS functionality: i.e. lock_hash / lock_hashref. The rest of functionality you need is a simple hash traversal and can be easily implemented manually. Traverse through nested references while keeping list of visited ones and list of found hashes and then run loop over that found list with lock_hashref.

like image 1
Oleg V. Volkov Avatar answered Oct 22 '22 19:10

Oleg V. Volkov