| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2012-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Struct::Dumb 0.16; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Scalar::Util qw( blessed refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # 'overloading.pm' was only added in 5.10 | ||||||||||||||||||||||||||||
| 16 | # Before that we can't easily implement forbidding of @{} overload, so lets not | ||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use constant HAVE_OVERLOADING => eval { require overloading }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use constant HAVE_FEATURE_CLASS => defined eval { require feature; $feature::feature{class} }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 11 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 22 | 11 | if( HAVE_FEATURE_CLASS ) { | |||||||||||||||||||||||||||
| 23 | # Before Perl 5.43.4, a class with no fields in it would cause thread | ||||||||||||||||||||||||||||
| 24 | # cloning to segfault. | ||||||||||||||||||||||||||||
| 25 | # https://github.com/Perl/perl5/issues/23771 | ||||||||||||||||||||||||||||
| 26 | 11 | my $dummy_field = ""; | |||||||||||||||||||||||||||
| 27 | 11 | ▶ | $dummy_field = 'field $dummy;' if $^V lt v5.43.4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | ▶ | my $ok = eval <<"EOF"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | { use experimental 'class'; class Struct::Dumb::Struct {$dummy_field}; } | ||||||||||||||||||||||||||||
| 31 | 1 | ||||||||||||||||||||||||||||
| 32 | EOF | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 10 | ▶ | die $@ unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | eval "{ package Struct::Dumb::Struct; }"; | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 NAME | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | C<Struct::Dumb> - make simple lightweight record-like structures | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use Struct::Dumb; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | struct Point => [qw( x y )]; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | my $point = Point(10, 20); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | printf "Point is at (%d, %d)\n", $point->x, $point->y; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | $point->y = 30; | ||||||||||||||||||||||||||||
| 56 | printf "Point is now at (%d, %d)\n", $point->x, $point->y; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Z<> | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | struct Point3D => [qw( x y z )], named_constructor => 1; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my $point3d = Point3D( z => 12, x => 100, y => 50 ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | printf "Point3d's height is %d\n", $point3d->z; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Z<> | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | struct Point3D => [qw( x y z )], predicate => "is_Point3D"; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my $point3d = Point3D( 1, 2, 3 ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | printf "This is a Point3D\n" if is_Point3D( $point3d ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Z<> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | use Struct::Dumb qw( -named_constructors ) | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | struct Point3D => [qw( x y z )]; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my $point3d = Point3D( x => 100, z => 12, y => 50 ); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | C<Struct::Dumb> creates record-like structure types, similar to the C<struct> | ||||||||||||||||||||||||||||
| 85 | keyword in C, C++ or C#, or C<Record> in Pascal. An invocation of this module | ||||||||||||||||||||||||||||
| 86 | will create a construction function which returns new object references with | ||||||||||||||||||||||||||||
| 87 | the given field values. These references all respond to lvalue methods that | ||||||||||||||||||||||||||||
| 88 | access or modify the values stored. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | It's specifically and intentionally not meant to be an object class. You | ||||||||||||||||||||||||||||
| 91 | cannot subclass it. You cannot provide additional methods. You cannot apply | ||||||||||||||||||||||||||||
| 92 | roles or mixins or metaclasses or traits or antlers or whatever else is in | ||||||||||||||||||||||||||||
| 93 | fashion this week. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | On the other hand, it is tiny, creates cheap lightweight array-backed | ||||||||||||||||||||||||||||
| 96 | structures, uses nothing outside of core. It's intended simply to be a | ||||||||||||||||||||||||||||
| 97 | slightly nicer way to store data structures, where otherwise you might be | ||||||||||||||||||||||||||||
| 98 | tempted to abuse a hash, complete with the risk of typoing key names. The | ||||||||||||||||||||||||||||
| 99 | constructor will C<croak> if passed the wrong number of arguments, as will | ||||||||||||||||||||||||||||
| 100 | attempts to refer to fields that don't exist. Accessor-mutators will C<croak> | ||||||||||||||||||||||||||||
| 101 | if invoked with arguments. (This helps detect likely bugs such as accidentally | ||||||||||||||||||||||||||||
| 102 | passing in the new value as an argument, or attempting to invoke a stored | ||||||||||||||||||||||||||||
| 103 | C<CODE> reference by passing argument values directly to the accessor.) | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | $ perl -E 'use Struct::Dumb; struct Point => [qw( x y )]; Point(30)' | ||||||||||||||||||||||||||||
| 106 | usage: main::Point($x, $y) at -e line 1 | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | $ perl -E 'use Struct::Dumb; struct Point => [qw( x y )]; Point(10,20)->z' | ||||||||||||||||||||||||||||
| 109 | main::Point does not have a 'z' field at -e line 1 | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | $ perl -E 'use Struct::Dumb; struct Point => [qw( x y )]; Point(1,2)->x(3)' | ||||||||||||||||||||||||||||
| 112 | main::Point->x invoked with arguments at -e line 1. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Objects in this class are (currently) backed by an ARRAY reference store, | ||||||||||||||||||||||||||||
| 115 | though this is an internal implementation detail and should not be relied on | ||||||||||||||||||||||||||||
| 116 | by using code. Attempting to dereference the object as an ARRAY will throw an | ||||||||||||||||||||||||||||
| 117 | exception. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | I<Note>: That on development perls that support C<use feature 'class'>, this | ||||||||||||||||||||||||||||
| 120 | is used instead of a blessed ARRAY reference. This implementation choice | ||||||||||||||||||||||||||||
| 121 | should be transparent to the end-user, as all the same features are supported. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 CONSTRUCTOR FORMS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | The C<struct> and C<readonly_struct> declarations create two different kinds | ||||||||||||||||||||||||||||
| 126 | of constructor function, depending on the setting of the C<named_constructor> | ||||||||||||||||||||||||||||
| 127 | option. When false, the constructor takes positional values in the same order | ||||||||||||||||||||||||||||
| 128 | as the fields were declared. When true, the constructor takes a key/value pair | ||||||||||||||||||||||||||||
| 129 | list in no particular order, giving the value of each named field. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | This option can be specified to the C<struct> and C<readonly_struct> | ||||||||||||||||||||||||||||
| 132 | functions. It defaults to false, but it can be set on a per-package basis to | ||||||||||||||||||||||||||||
| 133 | default true by supplying the C<-named_constructors> option on the C<use> | ||||||||||||||||||||||||||||
| 134 | statement. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | When using named constructors, individual fields may be declared as being | ||||||||||||||||||||||||||||
| 137 | optional. By preceeding the field name with a C<?> character, the constructor | ||||||||||||||||||||||||||||
| 138 | is instructed not to complain if a named parameter is not given for that | ||||||||||||||||||||||||||||
| 139 | field; instead it will be set to C<undef>. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | struct Person => [qw( name age ?address )], | ||||||||||||||||||||||||||||
| 142 | named_constructor => 1; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $bob = Person( name => "Bob", age => 20 ); | ||||||||||||||||||||||||||||
| 145 | # This is valid because 'address' is marked as optional | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub import | ||||||||||||||||||||||||||||
| 150 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 151 | 12 | my $pkg = shift; | |||||||||||||||||||||||||||
| 152 | 12 | my $caller = caller; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 12 | my %default_opts; | |||||||||||||||||||||||||||
| 155 | my %syms; | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 12 | foreach ( @_ ) { | |||||||||||||||||||||||||||
| 158 | 2 | ▶ | if( $_ eq "-named_constructors" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 1 | $default_opts{named_constructor} = 1; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 1 | $syms{$_}++; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 12 | ▶ | keys %syms or $syms{struct}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 12 | my %export; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 12 | ▶ | if( delete $syms{struct} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 14 | ▶ | $export{struct} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 14 | my ( $name, $fields, @opts ) = @_; | |||||||||||||||||||||||||||
| 173 | 14 | _struct( $name, $fields, scalar caller, lvalue => 1, %default_opts, @opts ); | |||||||||||||||||||||||||||
| 174 | 11 | }; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 12 | ▶ | if( delete $syms{readonly_struct} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | $export{readonly_struct} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 1 | my ( $name, $fields, @opts ) = @_; | |||||||||||||||||||||||||||
| 179 | 1 | _struct( $name, $fields, scalar caller, lvalue => 0, %default_opts, @opts ); | |||||||||||||||||||||||||||
| 180 | 1 | }; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 12 | ▶ | if( keys %syms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | croak "Unrecognised export symbols " . join( ", ", keys %syms ); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | 12 | *{"${caller}::$_"} = $export{$_} for keys %export; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 10 | my %_STRUCT_PACKAGES; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | sub _struct | ||||||||||||||||||||||||||||
| 198 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_struct: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 199 | 15 | my ( $name, $_fields, $caller, %opts ) = @_; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 15 | my $lvalue = !!$opts{lvalue}; | |||||||||||||||||||||||||||
| 202 | 15 | my $named = !!$opts{named_constructor}; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 15 | my $pkg = "${caller}::$name"; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 15 | my @fields = @$_fields; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 15 | my %optional; | |||||||||||||||||||||||||||
| 209 | 15 | ▶ | s/^\?// and $optional{$_}++ for @fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 15 | my %subs; | |||||||||||||||||||||||||||
| 212 | 15 | ▶ | $subs{DESTROY} = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 213 | 2 | ▶ | $subs{AUTOLOAD} = sub :lvalue { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 214 | 2 | my ( $field ) = our $AUTOLOAD =~ m/::([^:]+)$/; | |||||||||||||||||||||||||||
| 215 | 2 | croak "$pkg does not have a '$field' field"; | |||||||||||||||||||||||||||
| 216 | 0 | my $dummy; ## croak can't be last because it isn't lvalue, so this line is required | |||||||||||||||||||||||||||
| 217 | 15 | }; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 15 | my $constructor; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 15 | if( HAVE_FEATURE_CLASS ) { | |||||||||||||||||||||||||||
| 222 | 15 | _build_class_for_feature_class( $pkg, \@fields, \%optional, $named, $lvalue, \$constructor ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||
| 225 | _build_class_for_classical( $pkg, \@fields, \%optional, $named, $lvalue, \$constructor ); | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 229 | 15 | *{"${pkg}::$_"} = $subs{$_} for keys %subs; | |||||||||||||||||||||||||||
| 230 | 15 | *{"${caller}::$name"} = $constructor; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 15 | ▶ | if( my $predicate = $opts{predicate} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 1 | ▶ | *{"${caller}::$predicate"} = sub { ( ref($_[0]) || "" ) eq $pkg }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
ref $_[0] || ''
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table50%1/2
A
A= ref $_[0]
| |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 15 | ▶ | *{"${pkg}::_forbid_arrayification"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 237 | 1 | return if !HAVE_OVERLOADING and caller eq __PACKAGE__; | |||||||||||||||||||||||||||
| 238 | 1 | croak "Cannot use $pkg as an ARRAY reference" | |||||||||||||||||||||||||||
| 239 | 15 | }; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 15 | require overload; | |||||||||||||||||||||||||||
| 242 | $pkg->overload::OVERLOAD( | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | '@{}' => sub { $_[0]->_forbid_arrayification; return $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 244 | 1 | ▶ | '0+' => sub { refaddr $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 245 | 1 | ▶ | '""' => sub { sprintf "%s=Struct::Dumb(%#x)", $pkg, refaddr $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 1 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 247 | 15 | fallback => 1, | |||||||||||||||||||||||||||
| 248 | ); | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 15 | $_STRUCT_PACKAGES{$pkg} = { | |||||||||||||||||||||||||||
| 251 | named => $named, | ||||||||||||||||||||||||||||
| 252 | fields => \@fields, | ||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | sub _build_class_for_classical | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_class_for_classical: not called CC=5 SCAR=32.0
| |||||||||||||||||||||||||||||
| 258 | 0 | my ( $pkg, $fields, $optional, $named, $lvalue, $constructorvar ) = @_; | |||||||||||||||||||||||||||
| 259 | 0 | my @fields = @$fields; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if( $named ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $$constructorvar = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 263 | 0 | my %values = @_; | |||||||||||||||||||||||||||
| 264 | 0 | my @values; | |||||||||||||||||||||||||||
| 265 | 0 | foreach ( @fields ) { | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | exists $values{$_} or $optional->{$_} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $values{$_} or $optional->{$_}
MC/DC0%0/2
exists $values{$_} or $optional->{$_}
exists $values{$_}
$optional->{$_}
Truth table0%0/3
A or B
A= exists $values{$_}B= $optional->{$_}
| |||||||||||||||||||||||||||||
| 267 | croak "usage: $pkg requires '$_'"; | ||||||||||||||||||||||||||||
| 268 | 0 | push @values, delete $values{$_}; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if( my ( $extrakey ) = keys %values ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 0 | croak "usage: $pkg does not recognise '$extrakey'"; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 0 | bless \@values, $pkg; | |||||||||||||||||||||||||||
| 274 | 0 | }; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||
| 277 | 0 | my $fieldcount = @fields; | |||||||||||||||||||||||||||
| 278 | 0 | my $argnames = join ", ", map "\$$_", @fields; | |||||||||||||||||||||||||||
| 279 | 0 | ▶ | $$constructorvar = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 280 | 0 | ▶ | @_ == $fieldcount or croak "usage: $pkg($argnames)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | bless [ @_ ], $pkg; | |||||||||||||||||||||||||||
| 282 | 0 | }; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | my %subs; | |||||||||||||||||||||||||||
| 286 | 0 | foreach ( 0 .. $#fields ) { | |||||||||||||||||||||||||||
| 287 | 0 | my $idx = $_; | |||||||||||||||||||||||||||
| 288 | 0 | my $field = $fields[$idx]; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 10 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 291 | 10 | overloading->unimport if HAVE_OVERLOADING; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | $subs{$field} = $lvalue | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | ? sub :lvalue { @_ > 1 and croak "$pkg->$field invoked with arguments"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | shift->[$idx] } | |||||||||||||||||||||||||||
| 297 | 0 | ▶ | : sub { @_ > 1 and croak "$pkg->$field invoked with arguments"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | shift->[$idx] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 302 | 0 | *{"${pkg}::ISA"} = [ 'Struct::Dumb::Struct' ]; | |||||||||||||||||||||||||||
| 303 | 0 | *{"${pkg}::$_"} = $subs{$_} for keys %subs; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | sub _build_class_for_feature_class | ||||||||||||||||||||||||||||
| 307 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_class_for_feature_class: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 308 | 15 | my ( $pkg, $fields, $optional, $named, $lvalue, $constructorvar ) = @_; | |||||||||||||||||||||||||||
| 309 | 15 | my @fields = @$fields; | |||||||||||||||||||||||||||
| 310 | 15 | my %optional = %$optional; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 15 | ▶ | if( $named ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 5 | my %fieldnames = map { $_ => 1 } @fields; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 8 | ▶ | $$constructorvar = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 316 | 8 | my %values = @_; | |||||||||||||||||||||||||||
| 317 | 8 | foreach ( @fields ) { | |||||||||||||||||||||||||||
| 318 | 21 | ▶ | exists $values{$_} or $optional{$_} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $values{$_} or $optional{$_}
MC/DC100%2/2
exists $values{$_} or $optional{$_}
exists $values{$_}
$optional{$_}
Truth table100%3/3
A or B
A= exists $values{$_}B= $optional{$_}
| |||||||||||||||||||||||||||||
| 319 | croak "usage: $pkg requires '$_'"; | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | 7 | ▶ | $fieldnames{$_} or croak "usage: $pkg does not recognise '$_'" for keys %values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 6 | return $pkg->new( %values ); | |||||||||||||||||||||||||||
| 323 | 5 | }; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | else { | ||||||||||||||||||||||||||||
| 326 | 10 | my $fieldcount = @fields; | |||||||||||||||||||||||||||
| 327 | 10 | my $argnames = join ", ", map "\$$_", @fields; | |||||||||||||||||||||||||||
| 328 | 14 | ▶ | $$constructorvar = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 329 | 14 | ▶ | @_ == $fieldcount or croak "usage: $pkg($argnames)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 13 | my %values; @values{@fields} = @_; | |||||||||||||||||||||||||||
| 331 | 13 | return $pkg->new( %values ); | |||||||||||||||||||||||||||
| 332 | 10 | }; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 15 | ▶ | $lvalue = $lvalue ? " :lvalue" : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | my @fieldcode = map { | ||||||||||||||||||||||||||||
| 338 | 15 | my $name = $_; | |||||||||||||||||||||||||||
| 339 | 33 | ▶ | die "Field names are not permitted to contain ( or ) chars" if $name =~ m/[()]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # Mangle the field name until it has only valid identifiers | ||||||||||||||||||||||||||||
| 342 | # TODO: If someone tries to name a field foo_xDDbar then it might | ||||||||||||||||||||||||||||
| 343 | # conflict with a mangled one. | ||||||||||||||||||||||||||||
| 344 | 33 | my $var = '$' . $name =~ s/^(\P{ID_Start})/sprintf "_x%X", ord $1/er | |||||||||||||||||||||||||||
| 345 | 3 | =~ s/(\P{ID_Continue})/sprintf "_x%X", ord $1/ger; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 33 | ▶ | if( $var eq '$'.$name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 27 | " field $var :param = undef;", | |||||||||||||||||||||||||||
| 349 | " method $name$lvalue { \@_ and croak \"$pkg->$name invoked with arguments\"; $var }", | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | else { | ||||||||||||||||||||||||||||
| 352 | 6 | my $qname = quotemeta $name; | |||||||||||||||||||||||||||
| 353 | 6 | my $qqname = $name =~ s/"/\\"/gr; | |||||||||||||||||||||||||||
| 354 | 6 | " field $var :param($name) = undef;", | |||||||||||||||||||||||||||
| 355 | " { no strict 'refs'; *{\"${pkg}::${qname}\"} = method$lvalue { \@_ and croak \"$pkg->$qqname invoked with argument\"; $var } }", | ||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | } @$fields; | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 15 | my $code = join( "\n", | |||||||||||||||||||||||||||
| 360 | "use experimental 'class';", | ||||||||||||||||||||||||||||
| 361 | "class $pkg :isa(Struct::Dumb::Struct) {", | ||||||||||||||||||||||||||||
| 362 | " use Carp;", | ||||||||||||||||||||||||||||
| 363 | @fieldcode, | ||||||||||||||||||||||||||||
| 364 | "}", "" ); | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 15 | ▶ | unless( eval "$code; 1" ) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | 0 | my $e = $@; | |||||||||||||||||||||||||||
| 368 | 0 | $e =~ s/at \(eval \d+\) line \d+/"at eval() in ".__FILE__." line ".__LINE__/eg; | |||||||||||||||||||||||||||
| 369 | 0 | die $e; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =head2 struct | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | struct $name => [ @fieldnames ], | ||||||||||||||||||||||||||||
| 376 | named_constructor => (1|0), | ||||||||||||||||||||||||||||
| 377 | predicate => "is_$name"; | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | Creates a new structure type. This exports a new function of the type's name | ||||||||||||||||||||||||||||
| 380 | into the caller's namespace. Invoking this function returns a new instance of | ||||||||||||||||||||||||||||
| 381 | a type that implements those field names, as accessors and mutators for the | ||||||||||||||||||||||||||||
| 382 | fields. | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Takes the following options: | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =over 4 | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =item named_constructor => BOOL | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Determines whether the structure will take positional or named arguments. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =item predicate => STR | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | If defined, gives the name of a second function to export to the caller's | ||||||||||||||||||||||||||||
| 395 | namespace. This function will be a type test predicate; that is, a function | ||||||||||||||||||||||||||||
| 396 | that takes a single argmuent, and returns true if-and-only-if that argument is | ||||||||||||||||||||||||||||
| 397 | an instance of this structure type. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =back | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head2 readonly_struct | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | readonly_struct $name => [ @fieldnames ], | ||||||||||||||||||||||||||||
| 406 | ... | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Similar to L</struct>, but instances of this type are immutable once | ||||||||||||||||||||||||||||
| 409 | constructed. The field accessor methods will not be marked with the | ||||||||||||||||||||||||||||
| 410 | C<:lvalue> attribute. | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Takes the same options as L</struct>. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head2 dumper_info | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | my $info = Struct::Dumb::dumper_info( $struct ); | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | I<Since version 0.15.> | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | This function takes any instance of a Struct::Dumb-style struct and returns a | ||||||||||||||||||||||||||||
| 423 | hashref with the keys C<named>, C<fields>, and C<values>. "named" is a | ||||||||||||||||||||||||||||
| 424 | boolean, which is true if the struct uses a named constructor. "fields" is an | ||||||||||||||||||||||||||||
| 425 | arrayref giving the names of the struct's fields. "values" is an arrayref | ||||||||||||||||||||||||||||
| 426 | giving the values for those fields in corresponding order. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | This is just meant for tools like pretty printers to be able to inspect | ||||||||||||||||||||||||||||
| 429 | structures. We can't stop you from using it for nefarious purposes, but please | ||||||||||||||||||||||||||||
| 430 | don't. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | If the given C<$struct> is not an instance of a known class, an exception will | ||||||||||||||||||||||||||||
| 433 | be raised. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | This function is not exported. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =cut | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | sub dumper_info | ||||||||||||||||||||||||||||
| 440 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dumper_info: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 441 | 12 | my ( $struct ) = @_; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 11 | my $class = blessed $struct; | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 11 | ▶ | unless( defined $class and $struct->isa( 'Struct::Dumb::Struct' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $class and $struct->isa("Struct::Dumb::Struct")
MC/DC0%0/2
defined $class and $struct->isa("Struct::Dumb::Struct")
defined $class
$struct->isa("Struct::Dumb::Struct")
Truth table33%1/3
A and B
A= defined $classB= $struct->isa("Struct::Dumb::Struct")
| |||||||||||||||||||||||||||||
| 446 | 7 | croak "Struct::Dumb::dumper_info invoked with non-struct argument: $struct"; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | # Perl drops the leading `main::` in `main::StructName` so if there's no | ||||||||||||||||||||||||||||
| 450 | # `::` we'll have to put it back | ||||||||||||||||||||||||||||
| 451 | 6 | ▶ | $class =~ m/::/ or $class = "main::$class"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 452 | 6 | my $meta = $_STRUCT_PACKAGES{$class}; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 5 | ▶ | unless( $meta ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 1 | confess "Struct::Dumb::dumper_info encountered unknown struct class $class (this should not happen!)"; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 6 | my $fields = $meta->{fields}; | |||||||||||||||||||||||||||
| 459 | return { | ||||||||||||||||||||||||||||
| 460 | named => !!$meta->{named}, | ||||||||||||||||||||||||||||
| 461 | fields => [ @$fields ], | ||||||||||||||||||||||||||||
| 462 | 6 | values => [ map {; scalar $struct->$_ } @$fields ], | |||||||||||||||||||||||||||
| 463 | }; | ||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head1 DATA::DUMP FILTER | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | I<Since version 0.10.> | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | If L<Data::Dump> is loaded, an extra filter is applied so that struct | ||||||||||||||||||||||||||||
| 471 | instances are printed in a format matching that which would construct them. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | struct Colour => [qw( red green blue )]; | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | use Data::Dump; | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | my %hash = ( col => Colour( 0.8, 0.5, 0.2 ) ); | ||||||||||||||||||||||||||||
| 478 | Data::Dump::dd \%hash; | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | # prints {col => main::Colour(0.8, 0.5, 0.2)} | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =head1 NOTES | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head2 Allowing ARRAY dereference | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | The way that forbidding access to instances as if they were ARRAY references | ||||||||||||||||||||||||||||
| 487 | is currently implemented uses an internal method on the generated structure | ||||||||||||||||||||||||||||
| 488 | class called C<_forbid_arrayification>. If special circumstances require that | ||||||||||||||||||||||||||||
| 489 | this exception mechanism be bypassed, the method can be overloaded with an | ||||||||||||||||||||||||||||
| 490 | empty C<sub {}> body, allowing the struct instances in that class to be | ||||||||||||||||||||||||||||
| 491 | accessed like normal ARRAY references. For good practice this should be | ||||||||||||||||||||||||||||
| 492 | limited by a C<local> override. | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | For example, L<Devel::Cycle> needs to access the instances as plain ARRAY | ||||||||||||||||||||||||||||
| 495 | references so it can walk the data structure looking for reference cycles. | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | use Devel::Cycle; | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | { | ||||||||||||||||||||||||||||
| 500 | no warnings 'redefine'; | ||||||||||||||||||||||||||||
| 501 | local *Point::_forbid_arrayification = sub {}; | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | memory_cycle_ok( $point ); | ||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head1 TODO | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =over 4 | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =item * | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Consider adding an C<coerce_hash> option, giving name of another function to | ||||||||||||||||||||||||||||
| 513 | convert structs to key/value pairs, or a HASH ref. | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =back | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | =cut | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | sub maybe_apply_datadump_filter | ||||||||||||||||||||||||||||
| 524 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
maybe_apply_datadump_filter: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 525 | 3 | ▶ | return unless $INC{"Data/Dump.pm"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 2 | require Data::Dump::Filtered; | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 11 | ▶ | Data::Dump::Filtered::add_dump_filter( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 530 | 9 | my ( $ctx, $obj ) = @_; | |||||||||||||||||||||||||||
| 531 | 9 | ▶ | return undef unless $_STRUCT_PACKAGES{ $ctx->class }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 3 | my $dump = dumper_info( $obj ); | |||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | return { | ||||||||||||||||||||||||||||
| 536 | dump => sprintf "%s(%s)", $ctx->class, | ||||||||||||||||||||||||||||
| 537 | join ", ", map { | ||||||||||||||||||||||||||||
| 538 | ( $dump->{named} ? "$dump->{fields}[$_] => " : "" ) . | ||||||||||||||||||||||||||||
| 539 | 6 | ▶ | Data::Dump::dump($dump->{values}[$_]), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 540 | 3 | } 0 .. $#{$dump->{fields}}, | |||||||||||||||||||||||||||
| 541 | }; | ||||||||||||||||||||||||||||
| 542 | 2 | }); | |||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 10 | ▶ | if( defined &Data::Dump::dump ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 546 | 1 | maybe_apply_datadump_filter; | |||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||
| 548 | else { | ||||||||||||||||||||||||||||
| 549 | # A package var we observe that Data/Dump.pm seems to set when loaded | ||||||||||||||||||||||||||||
| 550 | # We can't attach to VERSION because too many other things get upset by | ||||||||||||||||||||||||||||
| 551 | # that. | ||||||||||||||||||||||||||||
| 552 | 9 | $Data::Dump::DEBUG = bless \( my $x = \&maybe_apply_datadump_filter ), | |||||||||||||||||||||||||||
| 553 | "Struct::Dumb::_DestroyWatch"; | ||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | { | ||||||||||||||||||||||||||||
| 557 | package Struct::Dumb::_DestroyWatch; | ||||||||||||||||||||||||||||
| 558 | 10 | my $GD = 0; | |||||||||||||||||||||||||||
| 559 | 9 | ▶ | END { $GD = 1 } | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 560 | 1 | ▶ | sub DESTROY { ${$_[0]}->() unless $GD; } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | 10 | 0x55AA; | |||||||||||||||||||||||||||