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 compilation of the eval
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 eval 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};
49 *{eval '\*CORE::GLOBAL::glob'} = $_[0];
52 sub _export_tags_into {
53 my ($class, $into, @tags) = @_;
54 foreach my $tag (@tags) {
56 tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
58 _set_glob(sub { \('<'.$_[0].'>'); });
60 foreach my $tag (@tags) {
62 delete ${"${into}::"}{$tag}
64 _set_glob(\&File::Glob::glob);
69 sub _install_unexporter {
70 my ($class, $unex) = @_;
71 $^H |= 0x120000; # localize %^H
72 $^H{'XML::Tags::Unex'} = bless($unex, 'XML::Tags::Unex');
75 package XML::Tags::TIEHANDLE;
77 sub TIEHANDLE { my $str = $_[1]; bless \$str, $_[0] }
78 sub READLINE { ${$_[0]} }
80 package XML::Tags::Unex;
82 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }