4 use warnings FATAL => 'all';
11 die "Can't import XML::Tags into a scope when already compiling one that uses it"
13 my ($class, @args) = @_;
14 my $opts = shift(@args) if ref($args[0]) eq 'HASH';
15 my $target = $class->_find_target(0, $opts);
16 my @tags = $class->_find_tags(@args);
17 my $unex = $class->_export_tags_into($target => @tags);
18 $class->_install_unexporter($unex);
23 map { # string == text -> HTML, scalarref == raw HTML, other == passthrough
25 ? (ref $_ eq 'SCALAR' ? $$_ : $_)
26 : do { local $_ = $_; # copy
27 s/&/&/g; s/"/"/g; s/</</g; s/>/>/g; $_;
32 sub _find_tags { shift; @_ }
35 my ($class, $extra_levels, $opts) = @_;
36 return $opts->{into} if defined($opts->{into});
37 my $level = ($opts->{into_level} || 1) + $extra_levels;
38 return (caller($level))[0];
42 # stupid insanity. delete anything already there so we disassociated
43 # the *CORE::GLOBAL::glob typeglob. Then the string reference call
44 # revivifies it - i.e. creates us a new glob, which we get a reference
45 # to, which we can then assign to.
46 # doing it without the quotes doesn't - it binds to the version in scope
47 # at compile time, which means after a delete you get a nice warm segv.
48 delete ${CORE::GLOBAL::}{glob};
50 *{'CORE::GLOBAL::glob'} = $_[0];
53 sub _export_tags_into {
54 my ($class, $into, @tags) = @_;
55 foreach my $tag (@tags) {
57 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
59 _set_glob(sub { \('<'.$_[0].'>'); });
61 foreach my $tag (@tags) {
63 delete ${"${into}::"}{$tag}
65 _set_glob(\&File::Glob::glob);
70 sub _install_unexporter {
71 my ($class, $unex) = @_;
72 $^H |= 0x120000; # localize %^H
73 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
76 package XML::Tags::TIEHANDLE;
78 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
79 sub READLINE { ${$_[0]} }
81 package XML::Tags::Unex;
83 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }