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
30 s/&/&/g; s/"/"/g; s/</</g; s/>/>/g; $_;
38 sub _find_tags { shift; @_ }
41 my ($class, $extra_levels, $opts) = @_;
42 return $opts->{into} if defined($opts->{into});
43 my $level = ($opts->{into_level} || 1) + $extra_levels;
44 return (caller($level))[0];
48 # stupid insanity. delete anything already there so we disassociated
49 # the *CORE::GLOBAL::glob typeglob. Then the string reference call
50 # revivifies it - i.e. creates us a new glob, which we get a reference
51 # to, which we can then assign to.
52 # doing it without the quotes doesn't - it binds to the version in scope
53 # at compile time, which means after a delete you get a nice warm segv.
54 delete ${CORE::GLOBAL::}{glob};
56 *{'CORE::GLOBAL::glob'} = $_[0];
59 sub _export_tags_into {
60 my ($class, $into, @tags) = @_;
61 foreach my $tag (@tags) {
63 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
66 local $XML::Tags::StringThing::IN_GLOBBERY = 1;
69 overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
71 foreach my $tag (@tags) {
73 delete ${"${into}::"}{$tag}
75 _set_glob(\&File::Glob::glob);
76 overload::remove_constant('q');
81 sub _install_unexporter {
82 my ($class, $unex) = @_;
83 $^H |= 0x120000; # localize %^H
84 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
87 package XML::Tags::TIEHANDLE;
89 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
90 sub READLINE { ${$_[0]} }
92 package XML::Tags::Unex;
94 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
96 package XML::Tags::StringThing;
108 ? XML::Tags::to_xml_string(@{$_[0]})
109 : (map +(ref $_ ? $$_ : $_), @{$_[0]})
115 my ($class, $initial, $parsed, $type) = @_;
116 return $parsed unless $type eq 'qq';
117 return $class->new($parsed);
121 my ($class, $string) = @_;
122 bless([ \$string ], $class);
126 my ($self, $other, $rev) = @_;
128 if (ref($other) && ($other =~ /[a-z]=[A-Z]/) && $other->isa(__PACKAGE__)) {
135 $rev ? unshift(@new, @extra) : push(@new, @extra);
136 bless(\@new, ref($self));