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.
| [reply] [d/l] |
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
| [reply] |
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.
| [reply] [d/l] |
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.
| [reply] [d/l] |
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
| [reply] [d/l] |
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
| [reply] [d/l] [select] |