Data::Lock - makes variables (im)?mutable
$Id: Lock.pm,v 0.4 2013/03/20 22:17:40 dankogai Exp dankogai $
use Data::Lock qw/dlock dunlock/; # note parentheses and equal dlock( my $sv = $initial_value ); dlock( my $ar = [@values] ); dlock( my $hr = { key => value, key => value, ... } ); dunlock $sv; dunlock $ar; dunlock \@av; dunlock $hr; dunlock \%hv;
dlock makes the specified variable immutable like Readonly. Unlike Readonly which implements immutability via tie, dlock makes use of the internal flag of perl SV so it imposes almost no penalty.
dlock
tie
Like Readonly, dlock locks not only the variable itself but also elements therein.
As of verion 0.03, you can dlock objects as well. Below is an example constructor that returns an immutable object:
sub new { my $pkg = shift; my $self = { @_ }; bless $self, $pkg; dlock($self); $self; }
Or consider using Moose.
Like List::Util and Scalar::Util, functions are exported only explicitly. This module comes with dlock and dunlock.
dunlock
use Data::Lock; # nothing imported; use Data::Lock qw/dlock dunlock/; # imports dlock() and dunlock()
dlock($scalar);
Locks $scalar and if $scalar is a reference, recursively locks referents.
Does the opposite of dlock.
Readonly, perlguts, perlapi
Dan Kogai, <dankogai+gmail at gmail.com>
<dankogai+gmail at gmail.com>
See Attribute::Constant.
Copyright 2008 Dan Kogai, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Attribute::Constant, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Attribute::Constant
CPAN shell
perl -MCPAN -e shell install Attribute::Constant
For more information on module installation, please visit the detailed CPAN module installation guide.