File Coverage

blib/lib/XML/Tags.pm
Criterion Covered Total %
statement 91 95 95.7
branch 18 26 69.2
condition 5 8 62.5
subroutine 24 24 100.0
pod 0 1 0.0
total 138 154 89.6


line stmt bran cond sub pod time code
1             package XML::Tags;
2              
3 2     2   23390 use strict;
  2         4  
  2         69  
4 2     2   9 use warnings FATAL => 'all';
  2         3  
  2         81  
5              
6 2     2   9 use File::Glob ();
  2         3  
  2         800  
7              
8             require overload;
9              
10             my $IN_SCOPE = 0;
11              
12             sub import {
13 11 50   11   133 die "Can't import XML::Tags into a scope when already compiling one that uses it"
14             if $IN_SCOPE;
15 11         82 my ($class, @args) = @_;
16 11 100       36 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
17 11         37 my $target = $class->_find_target(0, $opts);
18 11         36 my @tags = $class->_find_tags(@args);
19 11         49 my $unex = $class->_export_tags_into($target => @tags);
20 11 50       36 if ($INC{"bareword/filehandles.pm"}) { bareword::filehandles->import }
  0         0  
21 11         43 $class->_install_unexporter($unex);
22 11         244 $IN_SCOPE = 1;
23             }
24              
25             sub to_xml_string {
26             map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
27 25     25 0 39 ref($_)
28             ? (ref $_ eq 'SCALAR' ? $$_ : $_)
29 48 100       275 : do { local $_ = $_; # copy
  5 100       10  
30 5 50       11 if (defined) {
31 5         13 s/&/&/g; s/"/"/g; s//>/g; $_;
  5         11  
  5         11  
  5         8  
  5         9  
32             } else {
33 0         0 ''
34             }
35             }
36             } @_
37             }
38              
39 11     11   13 sub _find_tags { shift; @_ }
  11         151  
40              
41             sub _find_target {
42 11     11   17 my ($class, $extra_levels, $opts) = @_;
43 11 50       30 return $opts->{into} if defined($opts->{into});
44 11   100     41 my $level = ($opts->{into_level} || 1) + $extra_levels;
45 11         67 return (caller($level))[0];
46             }
47              
48             sub _set_glob {
49             # stupid insanity. delete anything already there so we disassociated
50             # the *CORE::GLOBAL::glob typeglob. Then the string reference call
51             # revivifies it - i.e. creates us a new glob, which we get a reference
52             # to, which we can then assign to.
53             # doing it without the quotes doesn't - it binds to the version in scope
54             # at compile time, which means after a delete you get a nice warm segv.
55 22     22   51 delete ${CORE::GLOBAL::}{glob};
56 2     2   12 no strict 'refs';
  2         2  
  2         178  
57 22         24 *{'CORE::GLOBAL::glob'} = $_[0];
  22         105  
58             }
59              
60             sub _export_tags_into {
61 11     11   79 my ($class, $into, @tags) = @_;
62 11         22 foreach my $tag (@tags) {
63 2     2   10 no strict 'refs';
  2         3  
  2         378  
64 624         523 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
  624         4508  
65             }
66             _set_glob(sub {
67 15     15   22 local $XML::Tags::StringThing::IN_GLOBBERY = 1;
68 15         29 \('<'."$_[0]".'>');
69 11         52 });
70 11     43   86 overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
  43         102  
71             return sub {
72 11     11   20 foreach my $tag (@tags) {
73 2     2   11 no strict 'refs';
  2         3  
  2         587  
74 624         520 delete ${"${into}::"}{$tag}
  624         1399  
75             }
76 11         26 _set_glob(\&File::Glob::glob);
77 11         33 overload::remove_constant('q');
78 11         92 $IN_SCOPE = 0;
79 11         284 };
80             }
81              
82             sub _install_unexporter {
83 11     11   17 my ($class, $unex) = @_;
84 11         18 $^H |= 0x20000; # localize %^H
85 11         78 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
86             }
87              
88             package XML::Tags::TIEHANDLE;
89              
90 624     624   575 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
  624         1245  
91 10     10   27 sub READLINE { ${$_[0]} }
  10         46  
92              
93             package XML::Tags::Unex;
94              
95 11 50   11   17 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
  11         17  
  11         21  
  11         2725  
96              
97             package XML::Tags::StringThing;
98              
99             use overload (
100 2         13 '.' => 'concat',
101             '""' => 'stringify',
102             fallback => 1
103 2     2   1924 );
  2         1264  
104              
105             sub stringify {
106             join(
107             '',
108             ((our $IN_GLOBBERY)
109 16         37 ? XML::Tags::to_xml_string(@{$_[0]})
110 17 50   17   34 : (map +(ref $_ ? $$_ : $_), @{$_[0]})
  1 100       13  
111             )
112             );
113             }
114              
115             sub from_constant {
116 43     43   94 my ($class, $initial, $parsed, $type) = @_;
117 43 100       236 return $parsed unless $type eq 'qq';
118 33         50 return $class->new($parsed);
119             }
120              
121             sub new {
122 33     33   41 my ($class, $string) = @_;
123 33         495 bless([ \$string ], $class);
124             }
125              
126             sub concat {
127 2     2   4 my ($self, $other, $rev) = @_;
128 2         3 my @extra = do {
129 2 50 66     13 if (ref($other) && ($other =~ /[a-z]=[A-Z]/) && $other->isa(__PACKAGE__)) {
      33        
130 0         0 @{$other}
  0         0  
131             } else {
132 2         6 $other;
133             }
134             };
135 2         5 my @new = @{$self};
  2         5  
136 2 50       7 $rev ? unshift(@new, @extra) : push(@new, @extra);
137 2         14 bless(\@new, ref($self));
138             }
139              
140             1;