Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Is there a way to lock arrays?

by tphyahoo (Vicar)
on Jul 07, 2005 at 20:45 UTC ( [id://473226]=perlquestion: print w/replies, xml ) Need Help??

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

Is there functionality for arrays similar to lock_keys with hashes? (eg, Re: Should I use Fields, InsideOuts, or Properties?)

my @array = qw(a b c); push @array, d; #should fail
I'm guessing there isn't, but I have some places in code where array is behaving in a way that it would be better if frozen. So... just on the off chance...

UPDATE: just found on my own Array::Lock. Maybe that's good enough.

Replies are listed 'Best First'.
Re: Is there a way to lock arrays?
by BrowserUk (Patriarch) on Jul 07, 2005 at 21:58 UTC

    Internals::SetReadOnly

    use Internals qw[SetReadOnly];; @a = 'a'..'c';; SetReadOnly \@a;; push @a, 'd';; Modification of a read-only value attempted at ...

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      Wouldn't that also prevent modification of values in the array? I'm not sure that's what the OP wants, but rather just to prevent the array from expanding. It's unclear, but the SetReadOnly function does come with the caveat that you can't modify the array at all.

      Larry Wall is Yoda: there is no try{}
      The Code that can be seen is not the true Code

        Sorry. I should have demonstrated that also.

        use Internals qw[SetReadOnly];; @a = 1 .. 3;; SetReadOnly \@a;; print @a;; 1 2 3 $a[ 2] = 'changed';; print @a;; 1 2 changed push @a, 'added';; Modification of a read-only value attempted at SetReadOnly \$a[1];; $a[1] = 'changed';; Modification of a read-only value attempted at

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Is there a way to lock arrays?
by hardburn (Abbot) on Jul 07, 2005 at 20:49 UTC

    It can be done with tied arrays. Just override the STORE() subroutine to do nothing, or throw an error. See perltie.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Is there a way to lock arrays?
by mrborisguy (Hermit) on Jul 07, 2005 at 20:47 UTC

    One way would be to write a sub that returned the array. I don't know what kind of performance hit that would have, but it should solve your problem, maybe.

    Otherwise, I'm sure you could do some tie magic, where you basically do nothing for STORE { ; } (or whatever is correct, haven't tied anything for a while, and even then didn't get too in-depth)

        -Bryan

Re: Is there a way to lock arrays?
by CountZero (Bishop) on Jul 07, 2005 at 20:52 UTC
    Just a guess: by overloading the STORE operator?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://473226]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found