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 if ($INC{"bareword/filehandles.pm"}) { bareword::filehandles->import }
21 $class->_install_unexporter($unex);
26 map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
28 ? (ref $_ eq 'SCALAR' ? $$_ : $_)
29 : do { local $_ = $_; # copy
31 s/&/&/g; s/"/"/g; s/</</g; s/>/>/g; $_;
39 sub _find_tags { shift; @_ }
42 my ($class, $extra_levels, $opts) = @_;
43 return $opts->{into} if defined($opts->{into});
44 my $level = ($opts->{into_level} || 1) + $extra_levels;
45 return (caller($level))[0];
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 delete ${CORE::GLOBAL::}{glob};
57 *{'CORE::GLOBAL::glob'} = $_[0];
60 sub _export_tags_into {
61 my ($class, $into, @tags) = @_;
62 foreach my $tag (@tags) {
64 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
67 local $XML::Tags::StringThing::IN_GLOBBERY = 1;
70 overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
72 foreach my $tag (@tags) {
74 delete ${"${into}::"}{$tag}
76 _set_glob(\&File::Glob::glob);
77 overload::remove_constant('q');
82 sub _install_unexporter {
83 my ($class, $unex) = @_;
84 $^H |= 0x120000; # localize %^H
85 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
88 package XML::Tags::TIEHANDLE;
90 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
91 sub READLINE { ${$_[0]} }
93 package XML::Tags::Unex;
95 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
97 package XML::Tags::StringThing;
109 ? XML::Tags::to_xml_string(@{$_[0]})
110 : (map +(ref $_ ? $$_ : $_), @{$_[0]})
116 my ($class, $initial, $parsed, $type) = @_;
117 return $parsed unless $type eq 'qq';
118 return $class->new($parsed);
122 my ($class, $string) = @_;
123 bless([ \$string ], $class);
127 my ($self, $other, $rev) = @_;
129 if (ref($other) && ($other =~ /[a-z]=[A-Z]/) && $other->isa(__PACKAGE__)) {
136 $rev ? unshift(@new, @extra) : push(@new, @extra);
137 bless(\@new, ref($self));