Parser.pm

filter: Statementstmt 77.2% 61 / 79 Branchbran 69.4% 25 / 36 Conditioncond 66.6% 20 / 30 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 71.6% 121 / 169 CC 33 SCAR 34.1
CC
33
Cov
73%
parse_file
47.0
parse
14.4
BEGIN
1.0
CRAP 30.2
« Document.pm ↑ Summary Element/Setting.pm »
1 package Org::Parser;
2  
3 24 ▶use 5.014; # compilation failure in older perls, RT#141560
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Org::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 24 our $DATE = '2026-07-21'; # DATE
11 24 our $DIST = 'Org-Parser'; # DIST
12 24 our $VERSION = '0.563'; # VERSION
13  
14 96 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.7
Pod documented
15 96 my ($self, $arg, $opts) = @_;
16 96 ▶ die "Please specify a defined argument to parse()\n" unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 2 94 96
17  
18 94 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
1381
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
19  
20 94 my $str;
21 94 my $srclabel = $opts->{_srclabel};
22 94 my $r = ref($arg);
23 94 ▶ if (!$r) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $r) { } 90 4 94
elsif ($r eq 'ARRAY') { } 1 3 4
elsif ($r eq 'GLOB' or builtin::blessed($arg) and $arg->isa('IO::Handle')) { } 1 2 3
elsif ($r eq 'CODE') { } 1 1 2
24 90 $str = $arg;
25 90 ▶ $srclabel //= "string";
Condition100%2/2
$srclabel //= "string"
l!l
1278
MC/DC100%1/1
$srclabel //= "string"
$srclabel
Truth table100%2/2
A
A= $srclabel
Aresult
11
00
26 } elsif ($r eq 'ARRAY') {
27 1 $str = join "", @$arg;
28 1 ▶ $srclabel //= "arrayref";
Condition50%1/2
$srclabel //= "arrayref"
l!l
01
MC/DC0%0/1
$srclabel //= "arrayref"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
29 ▶ } elsif ($r eq 'GLOB' || blessed($arg) && $arg->isa('IO::Handle')) {
Condition50%3/6
$r eq "GLOB" or builtin::blessed($arg) and $arg->isa("IO::Handle")
l!l&&r!l&&!r
102
builtin::blessed($arg) and $arg->isa("IO::Handle")
!ll&&!rl&&r
200
MC/DC33%1/3
$r eq "GLOB" or builtin::blessed($arg) and $arg->isa("IO::Handle")
$r eq "GLOB" builtin::blessed($arg) $arg->isa("IO::Handle")
Truth table50%2/4
A or B and C
A= $r eq "GLOB"B= builtin::blessed($arg)C= $arg->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
30 1 $str = join "", <$arg>;
31 1 ▶ $srclabel //= "filehandle";
Condition50%1/2
$srclabel //= "filehandle"
l!l
01
MC/DC0%0/1
$srclabel //= "filehandle"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
32 } elsif ($r eq 'CODE') {
33 1 my @chunks;
34 1 ▶ while (defined(my $chunk = $arg->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = &$arg())) 4 1 5
35 4 push @chunks, $chunk;
36 }
37 1 $str = join "", @chunks;
38 1 ▶ $srclabel //= "code";
Condition50%1/2
$srclabel //= "code"
l!l
01
MC/DC0%0/1
$srclabel //= "code"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
39 } else {
40 1 die "Invalid argument, please supply a ".
41 "string|arrayref|coderef|filehandle\n";
42 }
43 Org::Document->new(
44 _srclabel=>$srclabel,
45 from_string=>$str,
46 time_zone=>$opts->{time_zone},
47 ignore_unknown_settings=>$opts->{ignore_unknown_settings},
48 ignore_unknown_drawers =>$opts->{ignore_unknown_drawers},
49 93 );
50 }
51  
52 13 ▶sub parse_file {
Subroutine parse_file: called CC=20 SCAR=38.5
Pod documented
53 13 require File::Slurper;
54 13 my ($self, $filename, $opts) = @_;
55 13 ▶ $opts = {%$opts} if $opts; # shallow copy
Branch: 1 branch
BranchTrueFalseTotal
if $opts 3 10 13
56 13 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
310
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
57  
58 13 state $loaded;
59  
60 13 my $content = File::Slurper::read_text($filename);
61 13 $opts->{_srclabel} = "file:$filename";
62  
63 13 my $cf = $opts->{cache_file}; # old option, new option is 'cache' (automatic setting of cache file)
64 13 my $doc;
65 my $cache; # undef = no caching; 0 = not cached, should cache; 1 = cached
66 13 ▶ if (!$cf && ($opts->{cache} // $ENV{PERL_ORG_PARSER_CACHE})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}) 0 13 13
Condition50%3/6
not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
!ll&&!rl&&r
3100
$opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
l!l&&r!l&&!r
0010
MC/DC0%0/3
not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
not $cf $opts->{"cache"} $ENV{"PERL_ORG_PARSER_CACHE"}
Truth table50%2/4
not(A) and B // C
A= not $cfB= $opts->{"cache"}C= $ENV{"PERL_ORG_PARSER_CACHE"}
ABCresult
0XX0
1000
11X1
1011
67 0 require Cwd;
68 0 require Digest::MD5;
69 0 my @dirs = ("$ENV{HOME}/.cache/perl-org-parser", $ENV{HOME});
70 0 my $dir;
71 0 for (@dirs) {
72 0 ▶ if (-d $_) { $dir = $_; last }
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
elsif (mkdir $_) { } 0 0 0
73 0 elsif (mkdir $_) { $dir = $_; last }
74 }
75 0 ▶ die "Can't find a suitable cache directory" unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
76 0 ▶ my $abs = Cwd::abs_path($filename) or die "Can't find $filename";
Branch: 1 branch
BranchTrueFalseTotal
my $abs = Cwd::abs_path($filename) or die "Can't find $filename" 0 0 0
77 0 my $base = $abs; $base =~ s!.+/!!;
78 0 $cf = "$dir/$base.".Digest::MD5::md5_hex($abs).".storable";
79 }
80 13 ▶ if ($cf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf) 3 10 13
81 3 require Storable;
82 3 ▶ $cache = !!((-e $cf) && (-M $cf) <= (-M $filename));
Condition66%2/3
-e $cf && -M $cf <= -M $filename
!ll&&!rl&&r
021
MC/DC50%1/2
-e $cf && -M $cf <= -M $filename
-e $cf -M $cf <= -M $filename
Truth table66%2/3
A && B
A= -e $cfB= -M $cf <= -M $filename
ABresult
0X0
100
111
83 3 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 1 2 3
84 1 eval {
85 1 $doc = Storable::retrieve($cf);
86 1 ▶ $doc->load_element_modules unless $loaded++;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded++ 1 0 1
87 1 $doc->{_srclabel} = " (from cached file:$cf)";
88 };
89 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
90 0 warn "Failed retrieving document from cache: $@, reparsing ...";
91 0 $cache = 0;
92 }
93 }
94 }
95  
96 13 ▶ $doc = $self->parse($content, $opts) unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 12 1 13
97 12 ▶ if (defined($cache) && !$cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache and not $cache) 1 11 12
Condition100%3/3
defined $cache and not $cache
!ll&&!rl&&r
1011
MC/DC100%2/2
defined $cache and not $cache
defined $cache not $cache
Truth table100%3/3
A and not(B)
A= defined $cacheB= not $cache
ABresult
0X0
100
111
98 1 require Storable;
99 1 for ($doc->find('Timestamp')) {
100 0 $_->clear_parse_result;
101 }
102 1 Storable::store($doc, $cf);
103 }
104  
105 12 $doc;
106 }
107  
108 24 1;
109 # ABSTRACT: Parse Org documents
110  
111 __END__
« Document.pm ↑ Summary Element/Setting.pm »