GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  ATTRIBUTE::CONSTANT (3)

.ds Aq ’

NAME

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

CONTENTS

VERSION

$Id: Constant.pm,v 1.1 2013/04/03 14:37:57 dankogai Exp $

SYNOPSIS



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



DESCRIPTION

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

ATTRIBUTES

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

    Multi-line attributes

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



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



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



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



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

    Constants from Variables

You may be surprised the following code <B>DOES NOTB> work as you expected:



  #!/usr/bin/perl
  use strict;
  use warnings;
  use Attribute::Constant;
  use Data::Dumper;
  {
    package MyClass;
    sub new {
        my ( $class, %params ) = @_;
        return bless \%params, $class;
    }
  }
  my $o = MyClass->new( a => 1, b => 2 );
  my $x : Constant($o);
  print Dumper( $o, $x );



Which outputs:



  $VAR1 = bless( {
                 a => 1,
                 b => 2
               }, MyClass );
  $VAR2 = undef;



Why? Because $x : Constant($o) happens <B>beforeB> $o = Myclass->new().

On the other hand, the following works.



  my $y : Constant(MyClass->new(a => 1,b => 2));
  print Dumper( $o, $y );



Rule of the thumb is do not feed variables to constant because varialbes change after the attribute invocation.

Or simply use Data::Lock::dlock.



  use Data::Lock qw/dlock/;
  dlock my $z = $o;
  print Dumper( $o, $y );



SEE ALSO

Data::Lock, constant

AUTHOR

Dan Kogai, <dankogai+cpan at gmail.com>

BUGS & SUPPORT

See Data::Lock.

ACKNOWLEDGEMENTS

Readonly

COPYRIGHT & LICENSE

Copyright 2008-2013 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.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 ATTRIBUTE::CONSTANT (3) 2014-03-07

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.