Darren Chamberlain wrote:

* Tosh Cooey <tosh at 1200group.com> [2004/09/22 14:23]:


Hi Andy, would it be possible to add this update to the XML::Simple plugin?



This looks interesting, and I'd commit it <hint>if you included a test for it.</hint>

(darren)



Hi,
This looked like an interesting diversion. So, I cobbled up a patch and a test that I'm attaching to this message.


Kenny
#============================================================= -*-perl-*-
#
# t/xmlsimple.t
#
# Test the XML::Simple plugin.
#
# Written by Kenny Gatdula <[EMAIL PROTECTED]>
#
# Copyright (C) 2004 Kenny Gatdula.  All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id: skel.t,v 2.2 2000/09/12 15:25:24 abw Exp $
#
#========================================================================

use strict;
use lib qw( ./lib ../lib );
use Template::Test;
use Cwd qw( abs_path );
$^W = 1;

$Template::Test::DEBUG = 0;
#$Template::Test::DEBUG = 1;
#$Template::Parser::DEBUG = 1;
#$Template::Directive::PRETTY = 1;

my $tt1 = Template->new({
    INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
    ABSOLUTE => 1,
});

ok(1);

eval "use XML::Simple";


# account for script being run in distribution root or 't' directory
my $file = abs_path( -d 't' ? 't/test/xml' : 'test/xml' );
$file .= '/testfile.xml';

test_expect(\*DATA, $tt1, { 'xmlfile' => $file });

__END__
-- test --
[% TRY;
     USE xmlsimple = XML.Simple('no_such_file');
   CATCH;
     "ok";
   END
%]
-- expect --
ok


-- test --
[% USE xml = XML.Simple(xmlfile) -%]
[% xml.section.name -%]
-- expect --
alpha

-- test --
[% USE xs = XML.Simple -%]
[% xml = xs.XMLin( './test/xml/testfile.xml') -%]
[% xml.section.title -%]
-- expect --
The Alpha Zone

-- test --
[% USE xs = XML.Simple -%]
[% xml = xs.XMLin( './test/xml/testfile.xml') -%]
[% xmlout = xs.XMLout(xml) -%]
[% xmlout -%]
-- expect --
<opt id="webzone1">
  <section name="alpha" title="The Alpha Zone">
    <page href="/foo/bar" title="The Foo Page" />
    <page href="/bar/baz" title="The Bar Page" />
    <page href="/baz/qux" title="The Baz Page" />
  </section>
</opt>
--- Simple.pm.orig      Thu Sep 23 10:33:33 2004
+++ Simple.pm   Thu Sep 23 16:11:42 2004
@@ -44,7 +44,13 @@
     my $input   = shift;
     my $args    = ref $_[-1] eq 'HASH' ? pop(@_) : { };
 
-    XMLin($input, %$args);
+    if (defined($input)) {  
+        $input = $context->insert($input) unless ( $input =~ /</ );
+        return XMLin($input, %$args);
+    } else {
+        return new XML::Simple;
+    }
+
 }
 
 
@@ -84,7 +90,14 @@
 =head1 SYNOPSIS
 
     # load plugin and specify XML file to parse
-    [% USE xml = XML.Simple(xml_file_or_text) %]
+    [% USE xml = XML.Simple(xml_file_or_text) %] 
+
+    # Or load plugin as an object
+    [% USE xml = XML.Simple %]
+    # Then use XMLin or XMLout as usual
+    [% xml.XMLout(data_ref, args) %]
+    [% xml.XMLin(xml_file_or_text, args) %]
+
 
 =head1 DESCRIPTION
 

Reply via email to