in reply to equivalent to Hash::Util for lists / arrays

It can be done like this (using Data::Lock)
#!/usr/bin/perl use strict; use warnings; use Data::Lock qw(dlock); my @folks = qw(larry curly moe); dlock(\@folks); eval { shift @folks; print "removed larry\n"; } or warn $@; eval { $folks[2] = 'joe'; print "replaced moe\n"; } or warn $@;
output:
Modification of a read-only value attempted at /home/arun/test/dlock.p +l line 10. Modification of a read-only value attempted at /home/arun/test/dlock.p +l line 15.

Replies are listed 'Best First'.
Re^2: equivalent to Hash::Util for lists / arrays
by LanX (Saint) on Dec 21, 2010 at 16:29 UTC
    sure but Data::Lock is not core.

    If a real @array should be locked and unlocked in a core way, I would use Tie::Array to tie or untie it to a class "lock_array" with restricted modifying methods.

    IMHO anything faster is either not core or needs another interface for wrappers like inline functions resp. constants.

    UPDATE:

    Well ...

    Most I wrote was already elaborated in much detail in Readonly.pm.

    And an additional XS modul is also mentioned...

    Cheers Rolf