blib/lib/Data/Struct.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 80.7% 21 / 26 Conditioncond 50.0% 7 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 83.6% 102 / 122 CC 21 SCAR 17.8
CC
21
Cov
87%
struct
10.7
_build
6.0
_define
5.1
CRAP 5.9
↑ Summary
1 #! perl
2  
3 package Data::Struct;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 our @EXPORT = qw( &struct );
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 $Carp::Internal{ (__PACKAGE__) }++;
12  
13 3 our $VERSION = "1.701";
14  
15 # use Data::Struct;
16 #
17 # Definition (void context):
18 # struct Foo => qw(foo bar);
19 #
20 # Contructing (scalar context):
21 # my $s2 = struct "Foo";
22 # my $s3 = struct Foo => { foo => 1, bar => 2 };
23  
24 8 ▶sub struct {
Subroutine struct: called CC=10 SCAR=23.7
Pod undocumented
25 8 my @args = @_;
26 8 ▶ croak("'struct' needs at least a struct name") unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 8 8
27 8 my $struct = shift(@args);
28  
29 8 ▶ if ( !defined wantarray ) { # void context -> definition
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 3 5 8
30 # struct Foo => [ attr1, attr2, ... ]
31 3 ▶ if ( @args == 1 && ref($args[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "ARRAY") 0 3 3
Condition33%1/3
@args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
@args == 1 and ref $args[0] eq "ARRAY"
@args == 1 ref $args[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
32 0 return _define( $struct, $args[0] );
33 }
34  
35 # struct Foo => qw( attr1 attr2 ...)
36 3 ▶ return _define( $struct, \@args ) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 1 3
37 }
38  
39 6 ▶ if ( !wantarray ) { # scalar context -> construction
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 6 0 6
40 # $s = struct Foo => { attr1 => val1, attr2 = val2, ... }
41 6 ▶ if ( @args == 1 && ref($args[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "HASH") 2 4 6
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
402
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
42 2 my @ival = values %{ $args[0] };
43 2 @args = keys %{ $args[0] };
44 2 return _build( $struct, \@args, \@ival );
45 }
46  
47 # $s = struct foo;
48 4 ▶ return _build( $struct, [], [] ) unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 3 1 4
49 }
50  
51 1 croak("Ambiguous use of \"struct '$struct'\"");
52 }
53  
54 2 ▶sub _define {
Subroutine _define: called CC=5 SCAR=16.3
55 2 my ( $struct, $attrs ) = @_;
56  
57 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ${$struct.':: type'} = __PACKAGE__;
59  
60 # Undefined accessor catcher.
61 2 our $AUTOLOAD;
62 2 ▶ *{$struct.'::AUTOLOAD'} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
63 5 my ( $s, $a ) = $AUTOLOAD =~ /^(.*)::([^:]+)$/;
64 5 ▶ croak("Unknown accessor '$a' for struct '$s'")
Branch: 1 branch
BranchTrueFalseTotal
unless $a eq "DESTROY" 1 4 5
65 unless $a eq 'DESTROY';
66 2 };
67  
68 # Accessors.
69 2 foreach ( @$attrs ) {
70 4 ▶ croak("Invalid accessor name '$_' for struct '$struct'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and not ref $_ and /^[^\W\d]\w*$/s 0 4 4
Condition33%2/6
defined $_ and not ref $_ and /^[^\W\d]\w*$/s
!ll&&!rl&&r
004
defined $_ and not ref $_
!ll&&!rl&&r
004
MC/DC0%0/3
defined $_ and not ref $_ and /^[^\W\d]\w*$/s
defined $_ not ref $_ /^[^\W\d]\w*$/s
Truth table25%1/4
A and not(B) and C
A= defined $_B= not ref $_C= /^[^\W\d]\w*$/s
ABCresult
0XX0
10X0
1100
1111
71 unless defined and !ref and /^[^\W\d]\w*$/s;
72  
73 4 my $attr = $_; # lexical for closure
74  
75 4 ▶ *{$struct.'::'.$_} = sub () :lvalue {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 8 ▶ croak("Accessor '$attr' for struct '$struct' takes no arguments")
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 7 8
77 if @_ > 1;
78 7 $_[0]->{$attr};
79 4 };
80 };
81 }
82  
83 5 ▶sub _build {
Subroutine _build: called CC=6 SCAR=17.9
84 5 my ( $struct, $attrs, $values ) = @_;
85  
86 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 croak("Undefined struct '$struct'")
89 5 ▶ unless (${$struct . ":: type"}||'') eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
unless (${$struct . ':: type';} || "") eq __PACKAGE__ 1 4 5
Condition100%2/2
${$struct . ':: type';} || ''
l!l
41
MC/DC100%1/1
${$struct . ':: type';} || ''
${$struct . ':: type';}
Truth table100%2/2
A
A= ${$struct . ':: type';}
Aresult
11
00
90 4 ▶ Carp::confess("uneven") unless @$attrs == @$values;
Branch: 1 branch
BranchTrueFalseTotal
unless @$attrs == @$values 0 4 4
91  
92 # Construct empty struct.
93 4 my $s = bless {}, $struct;
94  
95 # Assign initial attributes, if any.
96 4 foreach ( @$attrs ) {
97 2 ▶ croak("Unknown accessor '$_' for struct '$struct'")
Branch: 1 branch
BranchTrueFalseTotal
unless $s->can($_) 1 1 2
98 unless $s->can($_);
99 1 $s->$_ = shift( @$values );
100 }
101  
102 3 return $s;
103 }
104  
105  
106 3 1;
107  
108 =pod
109  
110 =head1 NAME
111  
112 Data::Struct - Simple struct building
113  
114 =head1 SYNOPSIS
115  
116 use Data::Struct; # exports 'struct'
117  
118 # Define the struct and its accessors.
119 struct Foo => [ qw(foo bar) ];
120 struct Foo => qw(foo bar); # alternative
121  
122 # Construct the struct.
123 my $object = struct "Foo"; # empty struct
124 my $object = struct Foo => { bar => 1 };
125 my $object = struct Foo => { foo => "yes", bar => 1 };
126  
127 # Use it.
128 print "bar is " . $object->bar . "\n"; # 1
129 $object->bar = 2;
130 print "bar is now " . $object->bar . "\n"; # 2
131  
132 =head1 DESCRIPTION
133  
134 A I<struct> is a data structure that can contain values (attributes).
135 The values of the attributes can be set at creation time, and read and
136 modified at run time. This module implements a very basic and easy to
137 use I<struct> builder.
138  
139 Attributes can be anything that Perl can handle. There's no checking
140 on types. If you need I<struct>s with type checking and inheritance
141 and other fancy stuff, use one of the many CPAN modules that implement
142 data structures using classes and objects. Data::Struct deals with
143 data structures and not objects, so I placed this module under the
144 Data:: hierarchy.
145  
146 To use Data::Struct, just use it. This will export the struct()
147 function that does all the work.
148  
149 To define a structure, call struct() with the name of the structure and
150 a list of accessors to be created:
151  
152 struct( "Foo", "foo", "bar");
153  
154 which can be nicely written as:
155  
156 struct Foo => qw( foo bar );
157  
158 To prevent ambiguities, defining a struct requires struct() to be
159 called in void context.
160  
161 To create an empty structure:
162  
163 my $s = struct "Foo";
164  
165 To create a structure with one or more pre-initialised attributes:
166  
167 my $s = struct Foo => { foo => 3, bar => "Hi" };
168  
169 To prevent ambiguities, creating a struct requires struct() to be
170 called in scalar context.
171  
172 When the structure has been created, you can use accessor functions to
173 set and get the attributes:
174  
175 print "bar is " . $s->bar . "\n"; # "Hi"
176 $s->bar = 2;
177 print "bar is now " . $s->bar . "\n"; # 2
178  
179 =head1 PECULIARITIES
180  
181 Redefining a structure adds new attributes but leaves existing
182 attibutes untouched.
183  
184 struct "Foo" => qw(bar);
185 my $s = struct "Foo" => { bar => 2 };
186 struct Foo => qw(blech);
187 $s->blech = 4;
188 say $s->bar; # prints 2
189 say $s->blech; # prints 4
190  
191 This may change in a future version.
192  
193 =head1 SUPPORT
194  
195 Bugs should be reported via the CPAN bug tracker at
196  
197 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data::Struct>
198  
199 For other issues, contact the author.
200  
201 =head1 AUTHOR
202  
203 Johan Vromans E<lt>jv@cpan.orgE<gt>.
204  
205 =head1 SEE ALSO
206  
207 L<Object::Tiny>, L<Object::Tiny::Lvalue>, L<Object::Tiny::RW>,
208 L<Class::Struct>.
209  
210 =head1 COPYRIGHT
211  
212 Copyright 2011 Johan Vromans
213  
214 This program is free software; you can redistribute
215 it and/or modify it under the same terms as Perl itself.
216  
217 =cut
↑ Summary