perlxml-twig

Unable to update content in xml tag using XML::Twig


I am unable to update content in xml tag using xml Twig module.

Here is my xml file :-

<?xml version="1.0"?>
<test customerProprietary="false">
  <classes>
    <class name="new" />
  </classes>
  <chars>
    <log>false</log>
  </chars>
</test>

Code snippet:-

  my $twig = XML::Twig->new(
     pretty_print => 'indented',
     twig_handlers => {
             log   => sub {
                             if ($_->text eq 'true'){
                               exit;
                             }
                             else{
                               print $_->text;
                               subp4open($xmlfile);
                               $_->set_text( 'true' );
                               exit;
                            }
       }
);

$twig->parsefile($script_path);
$twig->print_to_file($script_path);

i have to update <log>false</log> to <log>true</log>. Am i missing something here?


Solution

  • That's because exit exits the program, so print_to_file is never reached. Remove the exits. You can replace them with returns, but they aren't needed.