| 1 | ### Data::PropertyList - Convert arbitrary objects to/from strings. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | ### Copyright 1996, 1997, 1998 Evolution Online Systems, Inc. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # You may use this software for free under the terms of the Artistic License | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | ### Change History | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # 1998-12-17 Minor doc cleanup; added makefile and tests for distribution. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # 1998-10-05 Tweaked output spacing for single-line string arrays. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # 1998-10-05 Switched from use of String::Escape::add to direct hash access. | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # 1998-07-23 Conventionalized POD, switched to yyyy.mm_dd version numbering. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # 1998-08-23 On further consideration, this really did belong in Data::*. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # 1998-06-11 Improved support for <<HERE strings. | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # 1998-05-07 Fixed problem with reading "0 = ..." lines in hashes. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # 1998-03-03 Replaced $r->class with ref($r) -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # 1998-02-28 Initialized _parse_multiline $value to '' to run clean under -w. | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # 1998-02-25 Version 1.00 - String::PropertyList | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # 1998-02-25 Moved to String:: and @EXPORT_OK for CPAN distribution - jeremy | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # 1998-01-28 Fixed variable name typo in _parse_array. | ||||||||||||||||||||||||||||||||||||||||||
| 19 | # 1998-01-11 Added rudimentary support for comments: full-line comments only | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # 1998-01-02 Renamed package Data::PropertyList to Text::PropertyList -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # 1997-12-08 Removed package Data::Types, use UNIVERSAL::isa instead. -Piglet | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # 1997-11-19 Added loopback handling to astext; now Supress as XREF TO | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # 1997-10-28 Updated to use new Text::Escape interface. | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # 1997-10-21 Documentation cleanup. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # 1997-08-17 Moved string escape/unescape code into new Text::Escape. -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # 1997-01-2? New fromDictionary parser -Eric | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # 1997-01-14 New asDictionary function provides closer match to NeXT style. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # 1997-01-11 Cloned & cleaned for Inetics; moved I/O to file.pm. V3.0 -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # 1996-10-29 Added append flag and trailing \n to write. -Piglet | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # 1996-08-06 Partial fix for blessed data; treat as basic type. V2.05 -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # 1996-07-13 Cleaned up flow, fixed headers. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # 1996-06-25 Wrote &write. V2.04 -EJ | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # 1996-06-23 Converted from Perl 4 library to Perl 5 package. V2.03 | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # 1996-06-18 Iterative line parsing replaces raw recursion. V2.02 | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # 1996-06-15 Clean start with support for nested data structures. V2.01 | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # 1996-05-26 Support for =<< multiline values. | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # 1996-05-08 Parse key-value pairs into a flat hash. Version 1. -Simon | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | package Data::PropertyList; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | require 5.003; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use vars qw( $VERSION @ISA @EXPORT_OK ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | $VERSION = 1998.12_17; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 3 | push @ISA, qw( Exporter ); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | push @EXPORT_OK, qw( astext fromtext ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | use vars qw( $Separator ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | $Separator = '.'; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | use String::Escape qw( qprintable unprintable ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | $String::Escape::Escapes{'astext'} = \&astext; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | $String::Escape::Escapes{'fromtext'} = \&fromtext; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | ### Writer | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | use vars qw( %DRefs %Supress $CurrentDRef $CurrentDepth ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | ▶ | use vars qw( $Indent $ShowClasses $ShowDRefs $Multiline $MaxItems ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | # $string = astext($value_or_reference); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # $string = astext($value_or_reference, %options); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # Write out an object graph in NeXT property list format | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # Numerous variables are localized, then we recurse. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 23 | ▶ | sub astext { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
astext: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 23 | my $target = shift; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 23 | my %options = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Options | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 23 | local $CurrentDRef = ''; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 23 | local $CurrentDepth = 0; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 23 | local $Indent = 2; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 23 | ▶ | local $ShowClasses = $options{'-classes'} if (exists $options{'-classes'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 23 | ▶ | local $ShowDRefs = $options{'-drefs'} if (exists $options{'-drefs'} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 23 | ▶ | local $Multiline = $options{'-multiline'} if (exists $options{'-multiline'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 23 | ▶ | local $MaxItems = $options{'-maxitems'} if (exists $options{'-maxitems'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # Working scope for this invokation of astext. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 23 | local %DRefs = (); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 23 | local %Supress = (); | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 23 | _astext( $target ) | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | # $string = _astext($referenceorvalue); | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 571 | ▶ | sub _astext { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_astext: called CC=40 SCAR=45.1
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 571 | my $target = shift; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # Write out an "UNDEFINED" comment to signal undefined values; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 571 | ▶ | return '/* UNDEFINED */' if (not defined $target); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # Write out escaped version of non-reference (string or number) values. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 571 | ▶ | if ( ! ref($target) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 557 | ▶ | if ( $Multiline and $target =~ /\n.*?\n/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Data::PropertyList::Multiline and $target =~ /\n.*?\n/
MC/DC0%0/2
$Data::PropertyList::Multiline and $target =~ /\n.*?\n/
$Data::PropertyList::Multiline
$target =~ /\n.*?\n/
Truth table33%1/3
A and B
A= $Data::PropertyList::MultilineB= $target =~ /\n.*?\n/
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return "<<END_OF_TEXT_DELIMITER\n" . | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | $target . ($target =~ /\n\Z/ ?'':"\n") . | ||||||||||||||||||||||||||||||||||||||||||
| 101 | " END_OF_TEXT_DELIMITER"; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 557 | return qprintable( $target ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # If this is a reference an item written out elsewhere, write an XREF comment | ||||||||||||||||||||||||||||||||||||||||||
| 108 | return '/* CROSS-REFERENCE TO ' . | ||||||||||||||||||||||||||||||||||||||||||
| 109 | ( length($DRefs{$target}) ? $DRefs{$target} : 'ROOT' ) .' */' | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 14 | ▶ | if ( exists $DRefs{$target} and $Supress{$target} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
exists $Data::PropertyList::DRefs{$target} and $Data::PropertyList::Supress{$target}
MC/DC0%0/2
exists $Data::PropertyList::DRefs{$target} and $Data::PropertyList::Supress{$target}
exists $Data::PropertyList::DRefs{$target}
$Data::PropertyList::Supress{$target}
Truth table66%2/3
A and B
A= exists $Data::PropertyList::DRefs{$target}B= $Data::PropertyList::Supress{$target}
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # Store a relative DRef from the root to here, if we haven't already | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 14 | ▶ | $DRefs{$target} = $CurrentDRef if ( not exists $DRefs{$target}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # We're going to show this item, so don't show it again in the future | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 14 | $Supress{$target} ++ ; | |||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # Variable to hold the stringified form of $target. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Write out DRef if $ShowDRefs is set | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 14 | ▶ | $result .= "/* DREF $CurrentDRef */ " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Data::PropertyList::ShowDRefs and length $Data::PropertyList::CurrentDRef
MC/DC100%2/2
$Data::PropertyList::ShowDRefs and length $Data::PropertyList::CurrentDRef
$Data::PropertyList::ShowDRefs
length $Data::PropertyList::CurrentDRef
Truth table100%3/3
A and B
A= $Data::PropertyList::ShowDRefsB= length $Data::PropertyList::CurrentDRef
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | if ($ShowDRefs and length $CurrentDRef); | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # Any DRefs after this point are separated by dots. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 14 | ▶ | local $CurrentDRef = $CurrentDRef . $Separator if ( length $CurrentDRef ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # Write out class of item if it's blessed and $ShowClasses is set | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 14 | ▶ | $result .= "/* CLASS " . ref($target) . " */ " if ($ShowClasses and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$Data::PropertyList::ShowClasses and ref $target and not ref($target) =~ /\A(ARRAY|HASH|SCALAR|REF|CODE)\Z/
$Data::PropertyList::ShowClasses and ref $target
MC/DC0%0/3
$Data::PropertyList::ShowClasses and ref $target and not ref($target) =~ /\A(ARRAY|HASH|SCALAR|REF|CODE)\Z/
$Data::PropertyList::ShowClasses
ref $target
not ref($target) =~ /\A(ARRAY|HASH|SCALAR|REF|CODE)\Z/
Truth table25%1/4
A and B and not(C)
A= $Data::PropertyList::ShowClassesB= ref $targetC= not ref($target) =~ /\A(ARRAY|HASH|SCALAR|REF|CODE)\Z/
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | ref($target) and (ref($target) !~ /\A(ARRAY|HASH|SCALAR|REF|CODE)\Z/)); | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 14 | ▶ | if ( UNIVERSAL::isa($target, 'HASH') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 6 | my $key; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 6 | foreach $key (sort keys %{$target}) { | |||||||||||||||||||||||||||||||||||||||||
| 135 | 12 | my $value = $target->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 12 | ▶ | next unless (ref $value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $DRefs{$value} = $CurrentDRef . $key unless ( exists $DRefs{$value} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $Supress{$value} ++; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 6 | ▶ | $result .= "{" if ($CurrentDepth); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 6 | ▶ | $result .= "\n" if ($result); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | $CurrentDepth ++; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 6 | foreach $key (sort keys %{$target}) { | |||||||||||||||||||||||||||||||||||||||||
| 144 | 12 | $result .= ' ' x ( ($CurrentDepth - 1) * $Indent); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 12 | local $CurrentDRef = $CurrentDRef . $key; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 12 | ▶ | $Supress{$target->{$key}} -- if ( ref $target->{$key} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 12 | $result .= _astext($key) . ' = ' . _astext($target->{$key}) .";\n"; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | $CurrentDepth --; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | ▶ | $result .= ' 'x(($CurrentDepth-1) * $Indent) . "}" if ($CurrentDepth); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 6 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | elsif ( UNIVERSAL::isa($target, 'ARRAY') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 8 | my $key; | |||||||||||||||||||||||||||||||||||||||||
| 156 | # If $MaxItems is set and there are fewer than that many non-ref items | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 8 | ▶ | my $one_line = ( $MaxItems and $#{$target} <= $MaxItems ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Data::PropertyList::MaxItems && $#{$target;} <= $Data::PropertyList::MaxItems
MC/DC50%1/2
$Data::PropertyList::MaxItems && $#{$target;} <= $Data::PropertyList::MaxItems
$Data::PropertyList::MaxItems
$#{$target;} <= $Data::PropertyList::MaxItems
Truth table66%2/3
A && B
A= $Data::PropertyList::MaxItemsB= $#{$target;} <= $Data::PropertyList::MaxItems
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 8 | foreach $key (0 .. $#{$target}) { | |||||||||||||||||||||||||||||||||||||||||
| 159 | 524 | my $value = $target->[$key]; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 524 | ▶ | next unless (ref $value); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | $one_line = 0; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 6 | ▶ | $DRefs{$value} = $CurrentDRef . $key unless ( exists $DRefs{$value} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | $Supress{$value} ++; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 8 | ▶ | my $joiner = ( $one_line ) ? " " : "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 8 | ▶ | $result .= "(" if ( $CurrentDepth ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 8 | ▶ | $result .= $joiner if ( $result ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | $CurrentDepth ++; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 8 | foreach $key (0 .. $#{$target}) { | |||||||||||||||||||||||||||||||||||||||||
| 170 | 524 | ▶ | $result .= $one_line ? '' : ' ' x ( ($CurrentDepth - 1) * $Indent); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 524 | local $CurrentDRef = $CurrentDRef . $key; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 524 | ▶ | $Supress{$target->[$key]} -- if ( ref $target->[$key] ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 524 | $result .= _astext($target->[$key]) . "," . $joiner; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 8 | $CurrentDepth --; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | $result .= ( ! $one_line ? ' 'x(($CurrentDepth-1) * $Indent) : '' ) . ")" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | if ( $CurrentDepth ); | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | ▶ | elsif (UNIVERSAL::isa($target, 'REF') or UNIVERSAL::isa($target, 'SCALAR')) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
UNIVERSAL::isa($target, "REF") or UNIVERSAL::isa($target, "SCALAR")
MC/DC0%0/2
UNIVERSAL::isa($target, "REF") or UNIVERSAL::isa($target, "SCALAR")
UNIVERSAL::isa($target, "REF")
UNIVERSAL::isa($target, "SCALAR")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($target, "REF")B= UNIVERSAL::isa($target, "SCALAR")
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | $result .= '/* REFERENCE */ '; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | local $CurrentDepth = $CurrentDepth + 1; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | local $CurrentDRef = $CurrentDRef . 0; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | $result .= _astext($$target); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # Otherwise it's some unsupported kind of reference; just "" stringify it | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return "/* REFERENCE TO $target */"; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | ### Reader | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 3 | ▶ | use vars qw( @TextLines $LineNumber $Source ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | # $datastructure = fromtext($string); | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # $datastructure = fromtext($string, %options); | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # reconstruct an object graph from a NeXT property list. | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 16 | ▶ | sub fromtext ($%) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fromtext: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 16 | my $dictionary_text = shift; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 16 | my %options = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 16 | local @TextLines = split("\n", $dictionary_text); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 16 | local $LineNumber = 0; | |||||||||||||||||||||||||||||||||||||||||
| 206 | 16 | ▶ | local $Source = $options{'-source'} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{'-source'} || ''
MC/DC100%1/1
$options{'-source'} || ''
$options{'-source'}
Truth table100%2/2
A
A= $options{'-source'}
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 16 | ▶ | if ( $options{'-scalar'} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 15 | return _parse_value( _get_line() . "\000", "\000" ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | } elsif ( $options{'-array'} ) { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | return _parse_array(); | |||||||||||||||||||||||||||||||||||||||||
| 212 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | return _parse_hash(); | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | # _parse_error( $message ); | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | sub _parse_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | my $message = shift; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | warn 'PropertyList error, ' . $message . | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | ' at line ' . $LineNumber . ( $Source ? ' in ' . $Source : '' ) ."\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | # $text = _get_line; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 27 | ▶ | sub _get_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 27 | $LineNumber++; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 27 | shift(@TextLines); | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | # $hash_ref = _parse_hash(); | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 3 | ▶ | sub _parse_hash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_hash: called CC=8 SCAR=22.6
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 3 | my $hash = {}; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 3 | my ($key, $value, $current_line); | |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | while (@TextLines) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 9 | $current_line = _get_line(); | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | # Ignore comments | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 9 | $current_line =~ s#\Q/*\E.*?\Q*/\E##g; | |||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | # Ignore blank lines | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 9 | ▶ | next if ( $current_line =~ /^\s*$/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # If we hit a closing brace, we're done with this hash | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 9 | ▶ | last if ( $current_line =~ /^\s*\}[,;]/o ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # Extract key and equals sign. | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 6 | ▶ | if ( $current_line =~ s/^\s*\"(([^\"\\]|\\.)+)\"//o ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $key = unprintable( $1 ); | |||||||||||||||||||||||||||||||||||||||||
| 250 | } elsif ( $current_line =~ s/^\s*(\S+)//o ) { | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 6 | $key = unprintable( $1 ); | |||||||||||||||||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | _parse_error("Key not found"); | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | 6 | ▶ | $current_line =~ s/^\s*=\s*//o or _parse_error("= not found"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # Extract value | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 6 | $value = _parse_value( $current_line, ';' ); | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | ▶ | next unless (defined $key); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | $hash->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 3 | return $hash; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | # $array_ref = _parse_array(); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 1 | ▶ | sub _parse_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_array: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | my $array = []; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | my ($value, $current_line); | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | while (@TextLines) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 3 | $current_line = _get_line(); | |||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | # Ignore comments | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 3 | $current_line =~ s#\Q/*\E.*?\Q*/\E##g; | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | # Ignore blank lines | ||||||||||||||||||||||||||||||||||||||||||
| 282 | 3 | ▶ | next if ( $current_line =~ /^\s*$/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | # If we hit a closing paren, we're done with this hash | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | last if ( $current_line =~ /^\s*\)[,;]/o ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | # Extract value | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 3 | $value = _parse_value( $current_line, ',' ); | |||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | push( @$array, $value); | |||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | return $array; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # $string = _parse_multiline($ender); | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub _parse_multiline { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_multiline: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | my $ender = shift; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my $value = ''; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my $current_line; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | while (@TextLines) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | $current_line = _get_line(); | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | last if ($current_line =~ /^\s*\Q$ender\E[\;\,]?\s*$/); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | $value .= $current_line . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | return $value; | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | # $value = _parse_value( $value, $terminator ); | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # Extracts a quoted or unquoted string, an array, hash, or a multiline string | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 24 | ▶ | sub _parse_value { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_value: called CC=7 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 24 | my $current_line = shift; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 24 | my $end_value = shift; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 24 | ▶ | if ( $current_line =~ /^\s*\"(([^\"\\]|\\.)*)\"\Q$end_value\E\s*/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | # Extract quoted value | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 10 | return unprintable( $1 ); | |||||||||||||||||||||||||||||||||||||||||
| 322 | } elsif ( $current_line =~ /^\s*(\S+?)\Q$end_value\E\s*/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 323 | # Extract unquoted value | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 11 | return unprintable( $1 ); | |||||||||||||||||||||||||||||||||||||||||
| 325 | } elsif ( $current_line =~ /^\s*(\/\*.*?\*\/)\s*\Q$end_value\E\s*/ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 326 | # Extract comment | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 328 | } elsif ( $current_line =~ /^\s*\{/o ) { | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 3 | return _parse_hash(); | |||||||||||||||||||||||||||||||||||||||||
| 330 | } elsif ( $current_line =~ /^\s*\(/o ) { | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | return _parse_array(); | |||||||||||||||||||||||||||||||||||||||||
| 332 | } elsif ( $current_line =~ /^\s*\<\<(\w+)(?:\Q$end_value\E)?/o ) { | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | return _parse_multiline($1); | |||||||||||||||||||||||||||||||||||||||||
| 334 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | _parse_error("value not found in '$current_line' - $end_value"); | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | __END__ | ||||||||||||||||||||||||||||||||||||||||||