| 1 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Data::Comparable; | ||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '1.100840'; | |||||||||||||||||||||||||||
| 7 | # ABSTRACT: Present your object for comparison purposes | ||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use UNIVERSAL::require; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use parent 'Data::Inherited'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 56 | ▶ | sub comparable_scalar { | ||||||||||||||||||||||||||
|
Subroutine
comparable_scalar: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 56 | my ($self, $scalar, $skip_bless) = @_; | |||||||||||||||||||||||||||
| 13 | 56 | my $class = ref $scalar; | |||||||||||||||||||||||||||
| 14 | 56 | ▶ | unless ($class) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Convert the value into a string, because eq_or_diff seems to make a | ||||||||||||||||||||||||||||
| 17 | # difference between strings and numbers. | ||||||||||||||||||||||||||||
| 18 | 40 | ▶ | return defined $scalar ? "$scalar" : $scalar; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Make sure the class this scalar is referencing is loaded. Suppose you | ||||||||||||||||||||||||||||
| 22 | # dump an object that has been serialized (say, from a database). Then it | ||||||||||||||||||||||||||||
| 23 | # could happen that the corresponding classes haven't been loaded, and so | ||||||||||||||||||||||||||||
| 24 | # it would dump incorrectly. | ||||||||||||||||||||||||||||
| 25 | 16 | ▶ | if ($class ne 'HASH' && $class ne 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$class ne "HASH" and $class ne "ARRAY"
MC/DC100%2/2
$class ne "HASH" and $class ne "ARRAY"
$class ne "HASH"
$class ne "ARRAY"
Truth table100%3/3
A and B
A= $class ne "HASH"B= $class ne "ARRAY"
| |||||||||||||||||||||||||||||
| 26 | 8 | ▶ | $class->require or die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | if (UNIVERSAL::can($scalar, 'prepare_comparable')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | $scalar->prepare_comparable; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 16 | ▶ | if (UNIVERSAL::can($scalar, 'comparable')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | 4 | return $scalar->comparable($skip_bless); | |||||||||||||||||||||||||||
| 33 | } elsif ($class eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 34 | 4 | return [ map { $self->comparable_scalar($_, $skip_bless) } @$scalar ]; | |||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # else it must be a hash - we don't support other forms of blessed | ||||||||||||||||||||||||||||
| 38 | # things yet. We could explicitly check for UNIVERSAL::isa($scalar, | ||||||||||||||||||||||||||||
| 39 | # 'HASH'), but that's too slow for the typical case where there are | ||||||||||||||||||||||||||||
| 40 | # huge structures composed of lists and possibly blessed hashes. | ||||||||||||||||||||||||||||
| 41 | 8 | my $hash; | |||||||||||||||||||||||||||
| 42 | 8 | ▶ | while (my ($key, $value) = each %$scalar) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 12 | $hash->{$key} = $self->comparable_scalar($value, $skip_bless); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # It could be an object of a class that doesn't implement comparable, | ||||||||||||||||||||||||||||
| 47 | # so we got into this branch, but we still want to return a properly | ||||||||||||||||||||||||||||
| 48 | # blessed object. | ||||||||||||||||||||||||||||
| 49 | 8 | ▶ | bless $hash, ref $scalar if ref $scalar ne 'HASH' && !$skip_bless; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $scalar ne "HASH" and not $skip_bless
MC/DC100%2/2
ref $scalar ne "HASH" and not $skip_bless
ref $scalar ne "HASH"
not $skip_bless
Truth table100%3/3
A and not(B)
A= ref $scalar ne "HASH"B= not $skip_bless
| |||||||||||||||||||||||||||||
| 50 | 8 | return $hash; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub comparable { | ||||||||||||||||||||||||||
|
Subroutine
comparable: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 4 | my ($self, $skip_bless) = @_; | |||||||||||||||||||||||||||
| 56 | 4 | ▶ | if (UNIVERSAL::can($self, 'prepare_comparable')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 4 | $self->prepare_comparable; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 4 | my %skip_keys = map { $_ => 1 } $self->every_list('SKIP_COMPARABLE_KEYS'); | |||||||||||||||||||||||||||
| 60 | 4 | my $copy = {}; | |||||||||||||||||||||||||||
| 61 | 4 | ▶ | while (my ($key, $value) = each %$self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 32 | ▶ | next if exists $skip_keys{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 28 | $copy->{$key} = $self->comparable_scalar($value, $skip_bless); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 4 | ▶ | bless $copy, ref $self unless $skip_bless; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 4 | return $copy; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub dump_comparable { | ||||||||||||||||||||||||||
|
Subroutine
dump_comparable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 2 | my ($self, $skip_bless) = @_; | |||||||||||||||||||||||||||
| 71 | 2 | require Data::Dumper; | |||||||||||||||||||||||||||
| 72 | 2 | local $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||
| 73 | 2 | Data::Dumper::Dumper($self->comparable($skip_bless)); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | sub yaml_dump_comparable { | ||||||||||||||||||||||||||
|
Subroutine
yaml_dump_comparable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 2 | my ($self, $skip_bless) = @_; | |||||||||||||||||||||||||||
| 78 | 2 | require YAML; | |||||||||||||||||||||||||||
| 79 | 2 | YAML::Dump($self->comparable($skip_bless)); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # So subclasses can call SUPER:: without worries. | ||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sub prepare_comparable { } | ||||||||||||||||||||||||||
|
Subroutine
prepare_comparable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 2 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||