Re: equivalent to Hash::Util for lists / arrays
by toolic (Bishop) on Dec 21, 2010 at 15:13 UTC
|
| [reply] |
|
|
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
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
| [reply] |
|
|
| [reply] |
|
|
> 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 ...
| [reply] |
Re: equivalent to Hash::Util for lists / arrays
by Arunbear (Prior) on Dec 21, 2010 at 16:18 UTC
|
#!/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.
| [reply] [d/l] [select] |
|
|
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...
| [reply] [d/l] |
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.
| [reply] [d/l] |