开发者

eXML-PARSER output contains unwanted hash references

So I wrote a parser routine to take one xml file and reparse into another one. This code I later modified to split a large xml file into many small xml files.

I am having a problem with an output. Parsing works fine the only thing output also includes unwanted strings like HASH(0x19f9b58), I am not sure why and need set of friendly eyes.

 use Encode;
    use XML::Parser;
    my $parser = XML::Parser->new( Handlers => {Start => \&handle_elem_start,
End => \&handle_elem_end,Char => \&handle_char_data,});
    my $record; 
    my $file = shift @ARGV;

    if( $file ) {$parser->parsefile( $file );} 
    exit;

    sub handle_elem_start 
    {
        my( $expat, $name, %atts ) = @_;

        if ($name eq 'articles'){$file="_data.xml";unlink($file);}
        $record .= "<";
        $record .= "$name";
        foreach my $key (keys %atts){$record .= " $key=\"$atts{$key}\"";}
        $record .= ">";
    }
    sub handle_char_data 
    {
        my( $expat, $text ) = @_;
        $text = decode_utf8( $text );
        $record .= "$text";
    }
    sub handle_elem_end 
    {
        my( $expat, $name ) = @_;
        $record .= "</$name>";
        if( $name eq 'article' )
        {
   开发者_如何学编程         open (MYFILE, '>>'.$file);
            print MYFILE $record;
            close (MYFILE);
            print $record;
            $record = {};
        }
        return unless( $name eq 'article' );
    }

Sample output:

...
</article>HASH(0x19f9b40)
<article doi="10.1103/PhysRevSeriesI.9.304">
<journal short="Phys. Rev. (Series I)" jcode="PRI">Physical Review (Series I)</journal>
<volume>9</volume>
<issue printdate="1899-11-00">5</issue>
<fpage>304</fpage>
<lpage>309</lpage>
<seqno>1</seqno>
<price></price><tocsec>Articles</tocsec>
<arttype type="article"></arttype><doi>10.1103/PhysRevSeriesI.9.304</doi>
<title>An Investigation of the Magnetic Qualities of Building Brick</title>
<authgrp>
<author><givenname>O.</givenname><middlename>A.</middlename><surname>Gage</surname></author>
<author><givenname>H.</givenname><middlename>E.</middlename><surname>Lawrence</surname></author>
</authgrp>
<cpyrt>
<cpyrtdate date="1899"></cpyrtdate><cpyrtholder>The American Physical Society</cpyrtholder>
</cpyrt>
</article>HASH(0x19f9b58)
...

HASH strings are not wanted, please advise.


$record = {};

sets $record to contain a reference to an empty hash. But everywhere else, you treat $record as a string, and append to it. When you treat a hashref as a string, you get a string like HASH(0x19f9b58) (the number varies).

You probably meant

$record = q{};

which sets $record to the empty string (just using alternate quotes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜