4 use warnings FATAL => 'all';
13 die "Can't import XML::Tags into a scope when already compiling one that uses it"
15 my ($class, @args) = @_;
16 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
17 my $target = $class->_find_target(0, $opts);
18 my @tags = $class->_find_tags(@args);
19 my $unex = $class->_export_tags_into($target => @tags);
20 $class->_install_unexporter($unex);
25 map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
27 ? (ref $_ eq 'SCALAR' ? $$_ : $_)
28 : do { local $_ = $_; # copy
29 s/&/&/g; s/"/"/g; s/</</g; s/>/>/g; $_;
34 sub _find_tags { shift; @_ }
37 my ($class, $extra_levels, $opts) = @_;
38 return $opts->{into} if defined($opts->{into});
39 my $level = ($opts->{into_level} || 1) + $extra_levels;
40 return (caller($level))[0];
44 # stupid insanity. delete anything already there so we disassociated
45 # the *CORE::GLOBAL::glob typeglob. Then the string reference call
46 # revivifies it - i.e. creates us a new glob, which we get a reference
47 # to, which we can then assign to.
48 # doing it without the quotes doesn't - it binds to the version in scope
49 # at compile time, which means after a delete you get a nice warm segv.
50 delete ${CORE::GLOBAL::}{glob};
52 *{'CORE::GLOBAL::glob'} = $_[0];
55 sub _export_tags_into {
56 my ($class, $into, @tags) = @_;
57 foreach my $tag (@tags) {
59 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
62 local $XML::Tags::StringThing::IN_GLOBBERY = 1;
65 overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
67 foreach my $tag (@tags) {
69 delete ${"${into}::"}{$tag}
71 _set_glob(\&File::Glob::glob);
72 overload::remove_constant('q');
77 sub _install_unexporter {
78 my ($class, $unex) = @_;
79 $^H |= 0x120000; # localize %^H
80 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
83 package XML::Tags::TIEHANDLE;
85 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
86 sub READLINE { ${$_[0]} }
88 package XML::Tags::Unex;
90 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
92 package XML::Tags::StringThing;
104 ? XML::Tags::to_xml_string(@{$_[0]})
105 : (map +(ref $_ ? $$_ : $_), @{$_[0]})
111 my ($class, $initial, $parsed, $type) = @_;
112 return $parsed unless $type eq 'qq';
113 return $class->new($parsed);
117 my ($class, $string) = @_;
118 bless([ \$string ], $class);
122 my ($self, $other, $rev) = @_;
124 if (ref($other) && ($other =~ /[a-z]=[A-Z]/) && $other->isa(__PACKAGE__)) {
131 $rev ? unshift(@new, @extra) : push(@new, @extra);
132 bless(\@new, ref($self));