File Coverage

blib/lib/ex/constant/vars.pm
Criterion Covered Total %
statement 98 98 100.0
branch 14 16 87.5
condition 2 3 66.6
subroutine 29 29 100.0
pod 1 3 33.3
total 144 149 96.6


line stmt bran cond sub pod time code
1             package ex::constant::vars;
2             $ex::constant::vars::VERSION = '0.07';
3 4     4   211518 use 5.006;
  4         38  
4 4     4   21 use strict;
  4         7  
  4         106  
5 4     4   23 use warnings;
  4         7  
  4         106  
6 4     4   19 use Carp;
  4         17  
  4         1836  
7              
8             require Exporter;
9              
10             our @ISA = qw(
11             Exporter
12             ex::constant::vars::scalar
13             ex::constant::vars::array
14             ex::constant::vars::hash
15             );
16             our @EXPORT_OK = qw( const SCALAR ARRAY HASH );
17              
18             sub const {
19 14     14 1 29 my $type = shift;
20 14         39 my @values = splice @_, 1;
21 14 100       68 if ( $type eq 'scalar' ) {
    100          
22 4         6 return tie ${$_[0]}, __PACKAGE__ . '::' . $type, @values;
  4         21  
23             } elsif ( $type eq 'array' ) {
24 5         9 return tie @{$_[0]}, __PACKAGE__ . '::' . $type, @values;
  5         27  
25             } else {
26 5         20 return tie %{$_[0]}, __PACKAGE__ . '::' . $type, @values;
  5         27  
27             }
28             }
29              
30 4     4   103 sub SCALAR (\$$) { 'scalar', @_ }
31 5     5 0 203 sub ARRAY (\@@) { 'array', @_ }
32 5     5 0 18 sub HASH (\%%) { 'hash', @_ }
33              
34             sub import {
35 6     6   51 my $self = shift;
36 6 100       1970 return unless @_;
37 4 100 66     26 if ( @_ == 1 && $_[0] eq 'const' ) {
38 2         2312 $self->export_to_level( 1, $self, @EXPORT_OK );
39             } else {
40 2         13 my %variables = @_;
41 2         4 my $caller = caller( 0 );
42 2         11 while ( my( $var, $val ) = each %variables ) {
43 7         23 my( $prefix, $name ) = split //, $var, 2;
44 7 50       36 croak "'$var' not a valid variable name" unless $prefix =~ /^[\$\@\%]$/;
45 7 100       24 if ( $prefix eq '$' ) {
    100          
    50          
46 4     4   34 no strict 'refs';
  4         9  
  4         481  
47 2         2 *{__PACKAGE__ . "::variables::$name"} = \$val;
  2         10  
48 2         3 *{"${caller}::$name"} = \${__PACKAGE__ . "::variables::$name"};
  2         5  
  2         7  
49 2         4 const SCALAR ${"${caller}::$name"}, $val;
  2         5  
50             } elsif ( $prefix eq '@' ) {
51 4     4   30 no strict 'refs';
  4         7  
  4         516  
52 2         4 *{__PACKAGE__ . "::variables::$name"} = \@{$val};
  2         21  
  2         4  
53 2         5 *{"${caller}::$name"} = \@{__PACKAGE__ . "::variables::$name"};
  2         15  
  2         6  
54 2         4 const ARRAY @{"${caller}::$name"}, @{$val};
  2         5  
  2         7  
55             } elsif ( $prefix eq '%' ) {
56 4     4   28 no strict 'refs';
  4         8  
  4         625  
57 3         5 *{__PACKAGE__ . "::variables::$name"} = \%{$val};
  3         24  
  3         6  
58 3         5 *{"${caller}::$name"} = \%{__PACKAGE__ . "::variables::$name"};
  3         8  
  3         10  
59 3         5 const HASH %{"${caller}::$name"}, %{$val};
  3         7  
  3         12  
60             }
61             }
62             }
63             }
64              
65              
66             package ex::constant::vars::scalar;
67             $ex::constant::vars::scalar::VERSION = '0.07';
68 4     4   30 use Carp;
  4         6  
  4         692  
69             $Carp::CarpLevel = 1;
70 7     7   222 sub TIESCALAR { shift; bless \(my $scalar = shift), __PACKAGE__ }
  7         44  
71 15     15   2526 sub FETCH { ${$_[0]} }
  15         80  
72 8     8   1292 sub STORE { croak "Modification of a read-only value attempted" }
73              
74              
75             package ex::constant::vars::array;
76             $ex::constant::vars::array::VERSION = '0.07';
77 4     4   37 use Carp;
  4         7  
  4         917  
78             $Carp::CarpLevel = 1;
79 7     7   14 sub TIEARRAY { shift; bless $_=\@_, __PACKAGE__ }
  7         49  
80 38     38   291 sub FETCH { $_[0]->[$_[1]] }
81 15     15   3225 sub FETCHSIZE { @{$_[0]} }
  15         50  
82 1     1   138 sub EXISTS { exists $_[0]->[$_[1]] }
83 13     13   1636 sub STORE { croak "Modification of a read-only value attempted" }
84             *CLEAR = *EXTEND = *POP = *PUSH = *SHIFT =
85             *UNSHIFT = *SPLICE = *STORESIZE = *DELETE = \*STORE;
86              
87              
88             package ex::constant::vars::hash;
89             $ex::constant::vars::hash::VERSION = '0.07';
90 4     4   43 use Carp;
  4         8  
  4         1454  
91             $Carp::CarpLevel = 1;
92 7     7   3449 sub TIEHASH { bless {@_[1...$#_]}, __PACKAGE__ }
93 56     56   349 sub FETCH { $_[0]->{$_[1]} }
94 19     19   4256 sub FIRSTKEY { keys %{$_[0]}; each %{$_[0]} }
  19         62  
  19         35  
  19         65  
95 56     56   74 sub NEXTKEY { each %{$_[0]} }
  56         155  
96 1     1   7 sub EXISTS { exists $_[0]->{$_[1]} }
97 15     15   1798 sub STORE { croak "Modification of a read-only value attempted" }
98             *CLEAR = *DELETE = \*STORE;
99              
100             1;
101              
102             __END__