Attribute::Constant - Make read-only variables via attribute


Attribute-Constant documentation  | view source Contained in the Attribute-Constant distribution.

Index


NAME

Top

Attribute::Constant - Make read-only variables via attribute

VERSION

Top

$Id: Constant.pm,v 0.2 2008/06/27 19:50:52 dankogai Exp dankogai $

SYNOPSIS

Top

 use Attribute::Constant;
 my $sv : Constant( $initial_value );
 my @av : Constant( @values );
 my %hv : Constant( key => value, key => value, ...);

DESCRIPTION

Top

This module uses Data::Lock to make the variable read-only. Check the document and source of Data::Lock for its mechanism.

ATTRIBUTES

Top

This module adds only one attribute, Constant. You give its initial value as shown. Unlike Readonly, parantheses cannot be ommited but it is semantically more elegant and thanks to Data::Lock, it imposes almost no performance penalty.

CAVEAT

Top

Multi-line attributes are not allowed in Perl 5.8.x.

  my $o : Constant(Foo->new(1,2,3)) # ok;
  my $p : Constant(Bar->new(
			    1,2,3
			   )
		  )                 # needs Perl 5.10

In which case you can use Data::Lock instead:

  dlock(my $p = Bar->new(
			 1, 2, 3
			));

After all, this module is a wrapper to Data::Lock;

BENCHMARK

Top

Here I have benchmarked like this.

  1.  Create an immutable variable.
  2.  try to change it and see if it raises exception
  3.  make sure the value stored remains unchanged.

See t/benchmark.pl for details.

Simple scalar
                Rate  Readonly      glob Attribute
  Readonly    7803/s        --      -97%      -97%
  glob      281666/s     3510%        --       -5%
  Attribute 295780/s     3691%        5%        --

Array with 1000 entries
                Rate  Readonly Attribute
  Readonly    8589/s        --      -97%
  Attribute 278755/s     3145%        --

Hash with 1000 key/value pairs
                Rate  Readonly Attribute
  Readonly    6979/s        --      -97%
  Attribute 207526/s     2874% 

SEE ALSO

Top

Data::Lock, constant

AUTHOR

Top

Dan Kogai, <dankogai at dan.co.jp>

BUGS

Top

Please report any bugs or feature requests to bug-attribute-constant at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Attribute-Constant. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Attribute::Constant

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Attribute-Constant

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Attribute-Constant

* CPAN Ratings

http://cpanratings.perl.org/d/Attribute-Constant

* Search CPAN

http://search.cpan.org/dist/Attribute-Constant

ACKNOWLEDGEMENTS

Top

Readonly

COPYRIGHT & LICENSE

Top


Attribute-Constant documentation  | view source Contained in the Attribute-Constant distribution.