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

Hi monks,

I have a problem getting XML::Parser to read a file of mine. I've stripped it down to a ridiculously bare-bones case:

use XML::Parser; sub handler_start() { print $_[1], "\n"; } my $parser = new XML::Parser(); $parser->setHandlers( Start => \&handler_start ); $parser->parsefile('test.xml');

And this is the content of the test.xml file:

<article id="1"> <tester>TEST</tester> </article>

Now, I can get XML::Parser to work on other snippets, but not this one. Is that XML malformed in some way I didn't realize? I am working on Windows (XP) and I get the error box "Perl Command Line Interpreter has encountered a problem and needs to close."

Can anybody see the problem?

Replies are listed 'Best First'.
Re: XML::Parser Weirdness
by DigitalKitty (Parson) on Jul 08, 2003 at 20:04 UTC
    Hi Anonymous Monk.

    I tested it on a win98 machine at work w/ perl 5.8.0 installed and I didn't experience any problems.

    Output:
    C:\Perl\bin\hw>perl test21.pl article tester C:\Perl\bin\hw>

    Since I'm not a regular win32 user, the only thing I can think of at the moment is you might need to install a service pack.

    Did you build your perl from source or are you using an ActiveState build?

    You might be interested in:

    Title: Perl & XML
    ISBN: 059600205X
    List Price: $34.95

    -or-

    Title: XML and Perl
    ISBN: 0735712891
    List Price: $39.99

    Hope this helps,
    -Katie.

      Heya DK

      This is activestate 5.6.1 with 2.27 of XML::Parser. The win machine is fully patched, but I was unable to replicate on either 5.8.0 machines or other 5.6.1 machines so it appears to be machine-specific. Hmm... maybe I need to *remove* a service pack or security path! :)

      Thanks for the help

Re: XML::Parser Weirdness
by gjb (Vicar) on Jul 08, 2003 at 19:09 UTC

    It seems to work just fine for me. I'm using Perl 5.8.0 that comes with cygwin on Windows XP. I should be using the latest version of XML::Parser and libxml version 2.5.7.

    Hope this helps, -gjb-

Re: XML::Parser Weirdness
by bigj (Monk) on Jul 08, 2003 at 23:14 UTC

    I don't think I found the reason for the occurring problem, but there is at least one odd part in your code:

    sub handler_start() { print $_1, "\n"; }
                     ^^
    

    With the () you prototype your subroutine. Thus, in doubt (e.g. calling the subroutine without brackets like print $x, $y, $z;), Perl will assume that your subroutine expects the specified kind of arguments — in your case no arguments.

    I really don't think that it is the problem, but it is at least a bad habit as your subroutine really wants arguments.

    Greetings,
    Janek

      I wasn't sure how to prototype something I was going to be passing by reference with&handler_start. Is it better to avoid prototyping here, or to prototype like this: sub handler_start; instead?

      Thanks!

        Is it better to avoid prototyping here, or to prototype like...

        It's best to avoid prototyping altogether unless you know what it does. In C a prototype tells the compiler, as well as anyone reading the code, what type of arguments a function expects and what type of value it returns. That is not what Perl prototypes do and not what they're for.

        Consider Perl's built-in 'push' function. It takes an array followed by a list of one or more scalars:

        push ARRAY, LIST

        If you tried to write a similar function yourself in Perl you might start it like this:

        sub my_push { my(@array, @list) = @_; ...

        But that's not going to work for two reasons:

        1. @array is now a copy of the array that was passed to your function, so nothing you do to it will affect the original
        2. the arguments to my_push have been flattened into a single array so all the values end up in @array and @list ends up empty

        Perl prototypes let you write subroutines that work the same way Perl functions do. They let you implicitly take a reference to the calling arguments and they let you avoid the problem of everything being flattened to a single array. A working version of my_push might start like this:

        sub my_push (\@@) { my($array_ref, @list) = @_; ...

        There are lots of subtleties with using prototypes (eg: in our example we do want the list of scalars to be flattened - hence no leading backslash on the second '@') so if you don't know what you're doing you can create problems that are hard to debug.

        It's unfortunate that this facility in Perl is called 'prototypes' when it really means an entirely different thing than what you might be familiar with that term meaning.

        Short story: if you're not trying to write subroutines that work like Perl's built-in functions then don't use prototypes at all.