Search code examples
xmlperlxml-twig

Attempt to bless into a reference while using XML::Twig


I am using the module XML::Twig for processing XML. While trying to run the nparse method with pretty_print option, I get an error. Following is the code:

use strict;
use XML::Twig;
use Data::Dumper;

my $xml_string = q{some xml string};
my $twig = XML::Twig->new();

$twig->nparse_pp( pretty_print => 'indented', $xml_string);

and the error message:

Attempt to bless into a reference at local/5.16/lib/perl5/XML/Twig.pm line 463.

Did I miss something?


Solution

  • Did I miss something

    • use strict; use warnings;
    • You never actually parse your $xml_string
    • you never declare or open $stdout. (Maybe you mean STDOUT)?
    • You use both XML::LibXML and XML::Twig.
    • nparse_pp implicitly sets pretty_print => 'indented' already.

    Post some code that actually compiles and runs, and we might be able to help.

    But perhaps you want something like;

    my $twig = XML::Twig -> parse ( $xml_string );
    $twig -> set_pretty_print ( 'indented' ); 
    $twig -> print;