Show
Ignore:
Timestamp:
06/28/08 04:51:24 (6 years ago)
Author:
dankogai
Message:

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

Location:
lang/perl/Attribute-Constant/trunk
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Attribute-Constant/trunk/Changes

    r14741 r14742  
    11# Revision history for Attribute-Constant 
    22# 
    3 # $Id: Changes,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: Changes,v 0.2 2008/06/27 19:50:52 dankogai Exp dankogai $ 
    44# 
    5 $Revision: 0.1 $ $Date: 2008/06/27 19:11:42 $ 
     5$Revision: 0.2 $ $Date: 2008/06/27 19:50:52 $ 
     6! lib/Attribute/Constant.pm  t/03-obj.t 
     7  Perl 5.8.x supported -- with caveats.  See POD 
     8 
     90.01 2008/06/27 19:11:42 
    610+ *  
    711  First Release. 
  • lang/perl/Attribute-Constant/trunk/Makefile.PL

    r14741 r14742  
    11# 
    2 # $Id: Makefile.PL,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     2# $Id: Makefile.PL,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    33# 
    44use 5.008001; 
  • lang/perl/Attribute-Constant/trunk/README

    r14741 r14742  
    11Attribute-Constant 
    22 
    3 $Id: README,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3$Id: README,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    44 
    55This distribution contains Attribute::Constant and Data::Lock. 
  • lang/perl/Attribute-Constant/trunk/lib/Attribute/Constant.pm

    r14741 r14742  
    33use warnings; 
    44use strict; 
    5 our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g; 
     5our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; 
    66use Attribute::Handlers; 
    77use Data::Lock (); 
     
    99sub UNIVERSAL::Constant : ATTR { 
    1010    my ( $pkg, $sym, $ref, $attr, $data, $phase ) = @_; 
    11     (     ref $ref eq 'HASH'  ? %$ref 
     11    ( 
     12          ref $ref eq 'HASH'  ? %$ref 
    1213        : ref $ref eq 'ARRAY' ? @$ref 
    13         :                      ($$ref) 
    14     ) = @$data; 
     14        : ($$ref) 
     15      ) = ref $data 
     16          ? ref $data eq 'ARRAY' ? @$data # perl 5.10.x 
     17          : $data : $data;                # perl 5.8.x 
    1518    Data::Lock::dlock($ref); 
    1619} 
     
    2528=head1 VERSION 
    2629 
    27 $Id: Constant.pm,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     30$Id: Constant.pm,v 0.2 2008/06/27 19:50:52 dankogai Exp dankogai $ 
    2831 
    2932=head1 SYNOPSIS 
     
    4548ommited but it is semantically more elegant and thanks to 
    4649L<Data::Lock>, it imposes almost no performance penalty. 
     50 
     51=head1 CAVEAT 
     52 
     53Multi-line attributes are not allowed in Perl 5.8.x. 
     54 
     55  my $o : Constant(Foo->new(1,2,3)) # ok; 
     56  my $p : Constant(Bar->new( 
     57                            1,2,3 
     58                           ) 
     59                  )                 # needs Perl 5.10 
     60 
     61In which case you can use L<Data::Lock> instead: 
     62 
     63  dlock(my $p = Bar->new( 
     64                         1, 2, 3 
     65                        )); 
     66 
     67After all, this module is a wrapper to L<Data::Lock>; 
    4768 
    4869=head1 BENCHMARK 
  • lang/perl/Attribute-Constant/trunk/lib/Data/Lock.pm

    r14741 r14742  
    33use warnings; 
    44use strict; 
    5 our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g; 
     5our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; 
    66 
    77use Attribute::Handlers; 
     
    3737        Internals::SvREADONLY( $_[0], $locked ); 
    3838    }; 
    39  
    4039} 
    4140 
     
    4948=head1 VERSION 
    5049 
    51 $Id: Lock.pm,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     50$Id: Lock.pm,v 0.2 2008/06/27 19:50:52 dankogai Exp dankogai $ 
    5251 
    5352=head1 SYNOPSIS 
  • lang/perl/Attribute-Constant/trunk/t/01-atomic.t

    r14741 r14742  
    11#!perl -T 
    22# 
    3 # $Id: 01-atomic.t,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: 01-atomic.t,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    44# 
    55use strict; 
  • lang/perl/Attribute-Constant/trunk/t/02-avhv.t

    r14741 r14742  
    11#!perl -T 
    22# 
    3 # $Id: 02-avhv.t,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: 02-avhv.t,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    44# 
    55use strict; 
  • lang/perl/Attribute-Constant/trunk/t/03-obj.t

    r14741 r14742  
    11#!perl -T 
    22# 
    3 # $Id: 03-obj.t,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: 03-obj.t,v 0.2 2008/06/27 19:50:52 dankogai Exp dankogai $ 
    44# 
    55use strict; 
     
    77use Attribute::Constant; 
    88#use Test::More 'no_plan'; 
    9 use Test::More tests => 5; 
     9use Test::More tests => 10; 
    1010 
    1111{ 
     
    1616} 
    1717{ 
    18     my $o : Constant( 
    19                      Foo->new(foo=>1) 
     18    my $o : Constant( Foo->new(foo=>1) ); 
     19    isa_ok $o, 'Foo'; 
     20    is $o->get, 1, '$o->get == 1'; 
     21    eval{ $o = Foo->new(foo=>2) }; 
     22    ok $@, $@; 
     23    eval{ $o->set(2) }; 
     24    ok !$@, '$o->set(2)'; 
     25    is $o->get, 2, '$o->get == 2'; 
     26} 
     27SKIP: { 
     28    skip 'Perl 5.9.5 or better required', 5 unless $] >= 5.009005; 
     29    my $o : Constant(  
     30                     Foo->new(foo=>1)  
    2031                    ); 
    2132    isa_ok $o, 'Foo'; 
  • lang/perl/Attribute-Constant/trunk/t/04-dlock.t

    r14741 r14742  
    11#!perl -T 
    22# 
    3 # $Id: 04-dlock.t,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: 04-dlock.t,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    44# 
    55use strict; 
  • lang/perl/Attribute-Constant/trunk/t/benchmark.pl

    r14741 r14742  
    11#!perl 
    22# 
    3 # $Id: benchmark.pl,v 0.1 2008/06/27 19:11:42 dankogai Exp dankogai $ 
     3# $Id: benchmark.pl,v 0.1 2008/06/27 19:11:42 dankogai Exp $ 
    44# 
    55use strict;