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   24725 use strict;
  2         4  
  2         57  
4 2     2   9 use warnings FATAL => 'all';
  2         4  
  2         69  
5              
6 2     2   10 use File::Glob ();
  2         5  
  2         1010  
7              
8             require overload;
9              
10             my $IN_SCOPE = 0;
11              
12             sub import {
13 11 50   11   94 die "Can't import XML::Tags into a scope when already compiling one that uses it"
14             if $IN_SCOPE;
15 11         69 my ($class, @args) = @_;
16 11 100       31 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
17 11         36 my $target = $class->_find_target(0, $opts);
18 11         36 my @tags = $class->_find_tags(@args);
19 11         51 my $unex = $class->_export_tags_into($target => @tags);
20 11 50       39 if ($INC{"bareword/filehandles.pm"}) { bareword::filehandles->import }
  0         0  
21 11         37 $class->_install_unexporter($unex);
22 11         186 $IN_SCOPE = 1;
23             }
24              
25             sub to_xml_string {
26             map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
27 25     25 0 35 ref($_)
28             ? (ref $_ eq 'SCALAR' ? $$_ : $_)
29 48 100       267 : do { local $_ = $_; # copy
  5 100       6  
30 5 50       10 if (defined) {
31 5         12 s/&/&/g; s/"/"/g; s//>/g; $_;
  5         9  
  5         9  
  5         9  
  5         10  
32             } else {
33 0         0 ''
34             }
35             }
36             } @_
37             }
38              
39 11     11   16 sub _find_tags { shift; @_ }
  11         131  
40              
41             sub _find_target {
42 11     11   15 my ($class, $extra_levels, $opts) = @_;
43 11 50       30 return $opts->{into} if defined($opts->{into});
44 11   100     37 my $level = ($opts->{into_level} || 1) + $extra_levels;
45 11         59 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   50 delete ${CORE::GLOBAL::}{glob};
56 2     2   12 no strict 'refs';
  2         4  
  2         187  
57 22         23 *{'CORE::GLOBAL::glob'} = $_[0];
  22         93  
58             }
59              
60             sub _export_tags_into {
61 11     11   68 my ($class, $into, @tags) = @_;
62 11         18 foreach my $tag (@tags) {
63 2     2   11 no strict 'refs';
  2         4  
  2         342  
64 624         659 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
  624         5242  
65             }
66             _set_glob(sub {
67 15     15   20 local $XML::Tags::StringThing::IN_GLOBBERY = 1;
68 15         30 \('<'."$_[0]".'>');
69 11         85 });
70 11     43   44 overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
  43         110  
71             return sub {
72 11     11   15 foreach my $tag (@tags) {
73 2     2   10 no strict 'refs';
  2         4  
  2         648  
74 624         594 delete ${"${into}::"}{$tag}
  624         1733  
75             }
76 11         26 _set_glob(\&File::Glob::glob);
77 11         31 overload::remove_constant('q');
78 11         81 $IN_SCOPE = 0;
79 11         347 };
80             }
81              
82             sub _install_unexporter {
83 11     11   17 my ($class, $unex) = @_;
84 11         19 $^H |= 0x20000; # localize %^H
85 11         64 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
86             }
87              
88             package XML::Tags::TIEHANDLE;
89              
90 624     624   761 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
  624         1498  
91 10     10   25 sub READLINE { ${$_[0]} }
  10         44  
92              
93             package XML::Tags::Unex;
94              
95 11 50   11   16 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
  11         17  
  11         22  
  11         2445  
96              
97             package XML::Tags::StringThing;
98              
99             use overload (
100 2         18 '.' => 'concat',
101             '""' => 'stringify',
102             fallback => 1
103 2     2   1474 );
  2         1004  
104              
105             sub stringify {
106             join(
107             '',
108             ((our $IN_GLOBBERY)
109 16         41 ? XML::Tags::to_xml_string(@{$_[0]})
110 17 50   17   30 : (map +(ref $_ ? $$_ : $_), @{$_[0]})
  1 100       10  
111             )
112             );
113             }
114              
115             sub from_constant {
116 43     43   79 my ($class, $initial, $parsed, $type) = @_;
117 43 100       246 return $parsed unless $type eq 'qq';
118 33         56 return $class->new($parsed);
119             }
120              
121             sub new {
122 33     33   59 my ($class, $string) = @_;
123 33         504 bless([ \$string ], $class);
124             }
125              
126             sub concat {
127 2     2   5 my ($self, $other, $rev) = @_;
128 2         4 my @extra = do {
129 2 50 66     10 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         3 my @new = @{$self};
  2         5  
136 2 50       7 $rev ? unshift(@new, @extra) : push(@new, @extra);
137 2         10 bless(\@new, ref($self));
138             }
139              
140             1;