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

Is there an equivalent to Hash::Util for arrays? I did see Readonly.pm, but it isn't part of the standard perl distribution and seems to suggest it is slow for arrays.
use Hash::Util; Hash::Util::lock_hash (%hash); Hash::Util::unlock_hash(%hash);

Replies are listed 'Best First'.
Re: equivalent to Hash::Util for lists / arrays
by toolic (Bishop) on Dec 21, 2010 at 15:13 UTC

      ...but I think neither of these two has the functionality the OP is specifically looking for, i.e. to lock an array against modification.

        The question was so poorly phrased that I was reluctant to reply, and IMHO toolic willently ignored it and chose the second best approach, the "anal tactic" of a "stubborn" reply... ;-)

        We can only speculate about the use case, but IIRC it's possible to define readonly lists with the constant pragma and use them with indices like arrays.

        UPDATE:

        use constant WEEKDAYS => qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\n";

        and it's also possible to use read-only values for arrays.

        Cheers Rolf

Re: equivalent to Hash::Util for lists / arrays
by CountZero (Bishop) on Dec 21, 2010 at 15:39 UTC
    There is of course no equivalent of the locking / unlocking of hash keys for an array.

    Locking the values can be achieved by the Readonly module, but I have yet to see a convincing use case for the locking of a whole array.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I have yet to see a convincing use case for the locking of a whole array.
      And have you seen it for hashes? I've a hard time imagining someone who can see a convincing case for the one, but not for the other.

      But regardless, whether or not some monks see convincing uses for it or not is irrelevant to the question on whether it exists or not.

        > I've a hard time imagining someone who can see a convincing case for the one, but not for the other.

        In theory yes, IMHO practically many of those cases wouldn't be implemented with arrays right away.

        For instance avoiding typos in keys - in cases where indices have to be reproduced correctly it's anyway better to use a hash.

        And if performance prohibits using hashes, there is nothing faster than using constant lists because of constant folding at compilation ...

        Cheers Rolf

Re: equivalent to Hash::Util for lists / arrays
by Arunbear (Prior) on Dec 21, 2010 at 16:18 UTC
    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.
      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

Re: equivalent to Hash::Util for lists / arrays
by BrowserUk (Patriarch) on Dec 21, 2010 at 23:04 UTC

    See Internals::SetReadOnly(). Combine with constant for all your constant needs far better than that other silly module.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.