use overload::constant to escape attributes within <foo ...>
Matt S Trout [Thu, 22 Oct 2009 18:26:20 +0000 (14:26 -0400)]
lib/XML/Tags.pm
t/tags.t

index 2637df7..0c9ff8b 100644 (file)
@@ -5,6 +5,8 @@ use warnings FATAL => 'all';
 
 use File::Glob ();
 
+require overload;
+
 my $IN_SCOPE = 0;
 
 sub import {
@@ -56,13 +58,18 @@ sub _export_tags_into {
     no strict 'refs';
     tie *{"${into}::${tag}"}, 'XML::Tags::TIEHANDLE', \"<${tag}>";
   }
-  _set_glob(sub { \('<'.$_[0].'>'); });
+  _set_glob(sub {
+    local $XML::Tags::StringThing::IN_GLOBBERY = 1;
+    \('<'."$_[0]".'>');
+  });
+  overload::constant(q => sub { XML::Tags::StringThing->from_constant(@_) });
   return sub {
     foreach my $tag (@tags) {
       no strict 'refs';
       delete ${"${into}::"}{$tag}
     }
     _set_glob(\&File::Glob::glob);
+    overload::remove_constant('q');
     $IN_SCOPE = 0;
   };
 }
@@ -82,4 +89,47 @@ package XML::Tags::Unex;
 
 sub DESTROY { local $@; eval { $_[0]->(); 1 } || warn "ARGH: $@" }
 
+package XML::Tags::StringThing;
+
+use overload (
+  '.' => 'concat',
+  '""' => 'stringify',
+  fallback => 1
+);
+
+sub stringify {
+  join(
+    '',
+    ((our $IN_GLOBBERY)
+      ? XML::Tags::to_xml_string(@{$_[0]})
+      : (map +(ref $_ ? $$_ : $_), @{$_[0]})
+    )
+  );
+}
+
+sub from_constant {
+  my ($class, $initial, $parsed, $type) = @_;
+  return $parsed unless $type eq 'qq';
+  return $class->new($parsed);
+}
+
+sub new {
+  my ($class, $string) = @_;
+  bless([ \$string ], $class);
+}
+
+sub concat {
+  my ($self, $other, $rev) = @_;
+  my @extra = do {
+    if (ref($other) && ($other =~ /[a-z]=[A-Z]/) && $other->isa(__PACKAGE__)) {
+      @{$other}
+    } else {
+      $other;
+    }
+  };
+  my @new = @{$self};
+  $rev ? unshift(@new, @extra) : push(@new, @extra);
+  bless(\@new, ref($self));
+}
+
 1;
index 9979e7f..747382e 100644 (file)
--- a/t/tags.t
+++ b/t/tags.t
@@ -25,6 +25,12 @@ use Test::More qw(no_plan);
     <html>, <body id="spoon">, "YAY", </body>, </html>;
   }
 
+  sub fleem {
+    use XML::Tags qw(woo);
+    my $ent = "one&two";
+    <woo ent="$ent">;
+  }
+
   sub globbery {
     <t/globbery/*>;
   }
@@ -51,6 +57,12 @@ is(
 );
 
 is(
+  join('', XML::Tags::to_xml_string Foo::fleem),
+  '<woo ent="one&amp;two">',
+  'Escaping ok'
+);
+
+is(
   join(', ', Foo::globbery),
   't/globbery/one, t/globbery/two',
   'real glob re-installed ok'