Neil Bowers > ex-constant-vars > ex::constant::vars

Download:
ex-constant-vars-0.05.tar.gz

Dependencies

Annotate this POD

Related Modules

Devel::Peek
more...
By perlmonks.org

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.05   Source  

NAME ^

ex::constant::vars - create readonly variables (alternative to constant pragma)

SYNOPSIS ^

Using the tie() interface:

  use ex::constant::vars;
  tie my $pi,     'ex::constant::vars', 4 * atan2( 1, 1 );
  tie my @family, 'ex::constant::vars', qw( John Jane );
  tie my %age,    'ex::constant::vars', John => 27,
                                        Jane => 'Back off!';

Using the const() function:

  use ex::constant::vars 'const';
  const SCALAR my $pi,     4 * atan2( 1, 1 );
  const ARRAY  my @family, qw( John Jane );
  const HASH   my %age,    John => 27, Jane => 'Back off!';

Using import() for compile time creation:

  use ex::constant::vars (
    '$pi'     => 4 * atan2( 1, 1 ),
    '@family' => [ qw( John Jane ) ],
    '%age'    => { John => 27, Jane => 'Back off!' },
  );

DESCRIPTION ^

This package allows you to create readonly variables. Unlike the constant pragma, this module lets you create readonly scalars, arrays and hashes.

This module tie()s variables to a class that disables any attempt to modify a variable's data.

Scalar

You cannot change the value in any way: not only assignment, but functions such as chomp and chop will fail.

Array

You cannot add to the array (unshift, push), remove from the array (shift, pop), nor change any values in the array.

Hash

You cannot change items in the hash, add new items to it, nor delete a key.

The const() function

When the const() function is imported, so are the helper functions SCALAR(), ARRAY(), and HASH(). These functions let const() know what type of variable it's dealing with. const() returns the tied() object of the variable.

Caveats ^

This implementation can be slow, by nature. tie()ing variables to a class is going to be slow. If you need the same functionality, and much less of a speed hit, take a look at this: http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-05/msg00777.html

The fastest method of declaring readonly variables with this pakcage is to tie() your variables. After that, using the const() function. And lastly, using import() at compile time.

To demonstrate the speed differences:

  use Benchmark; 
  timethese 500000, {
    constvars => sub {
                      tie my $x, 'ex::constant::vars', 'test';
                      my $y = $x;
                     },
    standard  => sub {
                      my $x = 'test';
                      my $y = $x;
                     },
  };

Produces:

 constvars: 24 wallclock secs (22.55 usr +  0.05 sys = 22.60 CPU) @ 22123.89/s (n=500000)
  standard:  2 wallclock secs ( 1.12 usr +  0.00 sys =  1.12 CPU) @ 447761.19/s (n=500000)

REPOSITORY ^

https://github.com/neilbowers/ex-constant-vars

AUTHOR ^

This module is now maintained by Neil Bowers <neil@bowers.com>.

It was originally written by Casey R. Tweten.

SEE ALSO ^

perl, perltie, constant.

COPYRIGHT ^

Copyright (c) 2012-2013 Neil Bowers. All rights reserved.

Copyright (c) 1995-2000 Casey R. Tweten. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: