loris has asked for the wisdom of the Perl Monks concerning the following question:

Hello XML-Datablocksters,

I am using __DATA__ for the first time and want to store XML in it and then parse the data with

my $parser = XML::LibXML->new(); my $xml = $parser->parse_fh(<DATA>);

However, I get the error

Usage: XML::LibXML::_parse_fh(self, fh, dir = &PL_sv_undef)

Is <DATA> not a file handle?

Puzzled,

loris

Replies are listed 'Best First'.
Re: Parsing XML in a __DATA__ Block
by gellyfish (Monsignor) on Jun 01, 2005 at 09:16 UTC

    No <DATA> is not a filehandle, it is an instance of the readline operator - DATA is a filehandle - you will probably want to do:

    my $parser = XML::LibXML->new(); my $xml = $parser->parse_fh(*DATA);

    /J\

      Funny, I always use \*DATA, but plain *DATA works equally well. Is there any difference between the 2?

        \*DATA is a real reference (typeglob reference), *DATA is not (bare typeglob).

        Those definitions extracted from `perldoc perlsub' (look for \*).

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        In most cases it doesn't matter, but it causes extra pain when you want to write a function that accurately needs to know "do I have a filehandle", like in File::Copy:
        my $from_a_handle = (ref($from) ? (ref($from) eq 'GLOB' || UNIVERSAL::isa($from, 'GLOB') || UNIVERSAL::isa($from, 'IO::Handle')) : (ref(\$from) eq 'GLOB'));

      Many thanks to you and mirod for the swift depuzzlement.

      loris

      Actually, one thing is still puzzling me.

      When I have

      __DATA__ ... XML here ... __END__

      I get the error

      Entity: line 13: parser error : Extra content at the end of the docume +nt __END__

      Shouldn't __END__ be recognised as not being part of the contents of the __DATA__ block?

      loris

        __END__ and __DATA__ are virtually synonyms, so that doesn't sound too practically feasable to me. Besides, what if you do want a line "__END__" inside your data, what then?

        Perhaps you should look into Inline::File.

        No

        /J\

Re: Parsing XML in a __DATA__ Block
by mirod (Canon) on Jun 01, 2005 at 09:14 UTC

    No, <DATA> reads from the filehandle *DATA and returns what it has read (delimited by $/).

    What you want is \*DATA, or locally undef $/ and use parse_string instead of parse_fh