| 1 | ## -*- Mode: CPerl -*- | ||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||
| 3 | ## File: DTA::CAB::Format::Perl.pm | ||||||||||||||||||||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||
| 5 | ## Description: Datum parser|formatter: perl code via Data::Dumper, eval() | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | package DTA::CAB::Format::Perl; | ||||||||||||||||||||
| 8 | 1 | ▶ | use DTA::CAB::Format; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use DTA::CAB::Datum ':all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Encode qw(encode decode); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | ##============================================================================== | ||||||||||||||||||||
| 17 | ## Globals | ||||||||||||||||||||
| 18 | ##============================================================================== | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | our @ISA = qw(DTA::CAB::Format); | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 1 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:prl|pl|perl|dump)$/); | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | ##============================================================================== | ||||||||||||||||||||
| 27 | ## Constructors etc. | ||||||||||||||||||||
| 28 | ##============================================================================== | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||||||||||||||||||||
| 31 | ## + object structure: assumed HASH | ||||||||||||||||||||
| 32 | ## ( | ||||||||||||||||||||
| 33 | ## ##---- Input | ||||||||||||||||||||
| 34 | ## doc => $doc, ##-- buffered input document | ||||||||||||||||||||
| 35 | ## | ||||||||||||||||||||
| 36 | ## ##---- Output | ||||||||||||||||||||
| 37 | ## dumper => $dumper, ##-- underlying Data::Dumper object | ||||||||||||||||||||
| 38 | ## | ||||||||||||||||||||
| 39 | ## ##---- INHERITED from DTA::CAB::Format | ||||||||||||||||||||
| 40 | ## level => $formatLevel, ##-- sets Data::Dumper->Indent() option | ||||||||||||||||||||
| 41 | ## outbuf => $stringBuffer, ##-- buffered output | ||||||||||||||||||||
| 42 | ## ) | ||||||||||||||||||||
| 43 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 44 | 0 | my $that = shift; | |||||||||||||||||||
| 45 | 0 | ▶ | my $fmt = bless({ | ||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 46 | ##-- I/O common | ||||||||||||||||||||
| 47 | utf8 => undef, ##-- n/a | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | ##-- Input | ||||||||||||||||||||
| 50 | #doc => undef, | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | ##-- Output | ||||||||||||||||||||
| 53 | dumper => Data::Dumper->new([])->Purity(1)->Terse(0)->Deepcopy(1), | ||||||||||||||||||||
| 54 | level => 0, | ||||||||||||||||||||
| 55 | outbuf => '', | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | ##-- user args | ||||||||||||||||||||
| 58 | @_ | ||||||||||||||||||||
| 59 | }, ref($that)||$that); | ||||||||||||||||||||
| 60 | 0 | return $fmt; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | ##============================================================================== | ||||||||||||||||||||
| 64 | ## Methods: Persistence | ||||||||||||||||||||
| 65 | ##============================================================================== | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | ## @keys = $class_or_obj->noSaveKeys() | ||||||||||||||||||||
| 68 | ## + returns list of keys not to be saved | ||||||||||||||||||||
| 69 | 0 | ▶ | sub noSaveKeys { | ||||||||||||||||||
|
Subroutine
noSaveKeys: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 70 | 0 | return ($_[0]->SUPER::noSaveKeys, qw(doc outbuf)); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | ##============================================================================== | ||||||||||||||||||||
| 74 | ## Methods: Input | ||||||||||||||||||||
| 75 | ##============================================================================== | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | ##-------------------------------------------------------------- | ||||||||||||||||||||
| 78 | ## Methods: Input: Input selection | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | ## $fmt = $fmt->close() | ||||||||||||||||||||
| 81 | 0 | ▶ | sub close { | ||||||||||||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 82 | 0 | delete($_[0]{doc}); | |||||||||||||||||||
| 83 | 0 | return $_[0]->SUPER::close(@_[1..$#_]); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | ## $fmt = $fmt->fromString( $string) | ||||||||||||||||||||
| 87 | ## $fmt = $fmt->fromString(\$string) | ||||||||||||||||||||
| 88 | 0 | ▶ | sub fromString { | ||||||||||||||||||
|
Subroutine
fromString: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 89 | 0 | my $fmt = shift; | |||||||||||||||||||
| 90 | 0 | $fmt->close(1); | |||||||||||||||||||
| 91 | 0 | ▶ | return $fmt->parsePerlString(ref($_[0]) ? $_[0] : \$_[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | ## $fmt = $fmt->fromFile($filename_or_handle) | ||||||||||||||||||||
| 95 | ## + default calls $fmt->fromFh() | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | ## $fmt = $fmt->fromFh($filename_or_handle) | ||||||||||||||||||||
| 98 | ## + default calls $fmt->fromString() on file contents | ||||||||||||||||||||
| 99 | 0 | ▶ | sub fromFh { | ||||||||||||||||||
|
Subroutine
fromFh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 100 | 0 | return shift->SUPER::fromFh_str(@_); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | ##-------------------------------------------------------------- | ||||||||||||||||||||
| 105 | ## Methods: Input: Local | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | ## $fmt = $fmt->parsePerlString(\$str) | ||||||||||||||||||||
| 108 | 0 | ▶ | sub parsePerlString { | ||||||||||||||||||
|
Subroutine
parsePerlString: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 109 | 0 | my $fmt = shift; | |||||||||||||||||||
| 110 | 0 | my ($doc); | |||||||||||||||||||
| 111 | 0 | ▶ | $doc = eval "no strict; ".(ref($_[0]) ? ${$_[0]} : $_[0]).";"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | ▶ | $fmt->warn("parsePerlString(): error in eval: $@") if ($@); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | $doc = DTA::CAB::Utils::deep_utf8_upgrade($doc); | |||||||||||||||||||
| 114 | 0 | ▶ | $fmt->{doc} = $fmt->{raw} ? $doc : $fmt->forceDocument($doc); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | return $fmt; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | ##-------------------------------------------------------------- | ||||||||||||||||||||
| 119 | ## Methods: Input: Generic API | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | ## $doc = $fmt->parseDocument() | ||||||||||||||||||||
| 122 | 0 | ▶ | sub parseDocument { return $_[0]{doc}; } | ||||||||||||||||||
|
Subroutine
parseDocument: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | ##============================================================================== | ||||||||||||||||||||
| 126 | ## Methods: Output | ||||||||||||||||||||
| 127 | ##============================================================================== | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | ##-------------------------------------------------------------- | ||||||||||||||||||||
| 130 | ## Methods: Output: Generic | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | ## $type = $fmt->mimeType() | ||||||||||||||||||||
| 133 | ## + override returns text/perl | ||||||||||||||||||||
| 134 | 0 | ▶ | sub mimeType { return 'text/perl'; } | ||||||||||||||||||
|
Subroutine
mimeType: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | ##-------------------------------------------------------------- | ||||||||||||||||||||
| 137 | ## Methods: Output: Generic API | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | ## $fmt = $fmt->putToken($tok) | ||||||||||||||||||||
| 140 | 0 | ▶ | sub putToken { | ||||||||||||||||||
|
Subroutine
putToken: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 141 | 0 | ▶ | $_[0]{fh}->print($_[0]{dumper}->Reset->Indent($_[0]{level}||0)->Names(['token'])->Values([$_[1]])->Dump."\$token\n"); | ||||||||||||||||||
|
Condition0%0/2
$_[0]{'level'} || 0
MC/DC0%0/1
$_[0]{'level'} || 0
$_[0]{'level'}
Truth table0%0/2
A
A= $_[0]{'level'}
| |||||||||||||||||||||
| 142 | 0 | return $_[0]; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | ## $fmt = $fmt->putSentence($sent) | ||||||||||||||||||||
| 146 | 0 | ▶ | sub putSentence { | ||||||||||||||||||
|
Subroutine
putSentence: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 147 | 0 | ▶ | $_[0]{fh}->print($_[0]{dumper}->Reset->Indent($_[0]{level}||0)->Names(['sentence'])->Values([$_[1]])->Dump."\$sentence\n"); | ||||||||||||||||||
|
Condition0%0/2
$_[0]{'level'} || 0
MC/DC0%0/1
$_[0]{'level'} || 0
$_[0]{'level'}
Truth table0%0/2
A
A= $_[0]{'level'}
| |||||||||||||||||||||
| 148 | 0 | return $_[0]; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | ## $fmt = $fmt->putDocument($doc) | ||||||||||||||||||||
| 152 | 0 | ▶ | sub putDocument { | ||||||||||||||||||
|
Subroutine
putDocument: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 153 | 0 | ▶ | $_[0]{fh}->print($_[0]{dumper}->Reset->Indent($_[0]{level}||0)->Names(['document'])->Values([$_[1]])->Dump."\$document\n"); | ||||||||||||||||||
|
Condition0%0/2
$_[0]{'level'} || 0
MC/DC0%0/1
$_[0]{'level'} || 0
$_[0]{'level'}
Truth table0%0/2
A
A= $_[0]{'level'}
| |||||||||||||||||||||
| 154 | 0 | return $_[0]; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | 1; ##-- be happy | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||