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

Hi Monks!

I'm having a slight understanding problem pertaining the use of external modules, packages and the scope of objects/functions (not really sure what the right terms are, so i'm just putting everything under the same hood).

Basically, i want to use package XML::LibXML. So what I do is write use XML::LibXML; at the beginning of the file. So far so good.

Then I want to call the new() function to create a new parser, and I can fully understand the need for the XML::LibXML::new() notation.

Then I want to call function parse_html_file from this object. I'd have expected the interpreter to be able to recognize that, using an object of type XML::LibXML::Parser, $xmlparser->parse_html_file( $file ) refers to this function. However, when I write it this way, then I get an error:
Can't locate object method "parse_html_file" via package "main" at tes +txml.pl line 25.
But well, let's be well-mannered and write $xmlparser->XML::LibXML::parse_html_file( $file ). Oh joy, the error has vanished! And has been replaced by another one:
Can't locate object method "_init_callbacks" via package "main" at /us +r/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi/XML/LibXML.pm +line 823.
which I now fail to understand. I've been trying to add some XML::LibXML at various places that might have seemed logical to me, needless to say without any luck.

Incidentally, executing the following script with the -w option raises some warnings from LibXML.pm, but I assume they don't have any part in my current problem?
#!/usr/bin/perl -w use strict; use XML::LibXML; my $file = shift; my $xmlparser; my $doc; $xmlparser = XML::LibXML::new(); $doc = $xmlparser->XML::LibXML::parse_html_file( $file ) or die "unabl +e to parse $file: $@";
And here's the output:
enigma:~/projects/pt] pagod% perl testxml1.pl Use of uninitialized value $class in bless at /usr/lib/perl5/site_perl +/5.10.0/x86_64-linux-thread-multi/XML/LibXML.pm line 191. Explicit blessing to '' (assuming package main) at /usr/lib/perl5/site +_perl/5.10.0/x86_64-linux-thread-multi/XML/LibXML.pm line 191. Can't locate object method "_init_callbacks" via package "main" at /us +r/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi/XML/LibXML.pm +line 823. [enigma:~/projects/pt] pagod%
So I guess these problems are solely due to my lack of experience with using modules and packages and stuff. So perhaps someone might like to explain to me what I'm doing wrong and how I could do it right?

Thanx a lot in advance!

Pagod

Replies are listed 'Best First'.
Re: scope and packages
by davorg (Chancellor) on Jun 26, 2009 at 09:51 UTC

    The best approach is probably to follow the examples in the documentation.

    use XML::LibXML; my $parser = XML::LibXML->new; my $doc = $parser->parse_html_file($file);

    Something along those lines should work. Looks like you were calling XML::LibXML::new rather than XML::LibXML->new.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re: scope and packages
by ELISHEVA (Prior) on Jun 26, 2009 at 10:34 UTC
    A small bit of explanation to supplement what is above: X->new(...) (with the arrow) does two things:
    • It causes X to be passed as an implicit parameter, as if you had called new('X',...). Because no class was being passed into new, the routine to create an object bless($someref, $someclass) was called with $someclass set to undef as its class parameter. This causes Perl to bless the object into 'main': hence the cryptic messages about main not containing a definition for the method. - see bless.
    • It causes Perl to search the class and superclass definitions for a definition of new(...) This can come in handy when you want to add some methods to an existing class, but are happy with the way the existing class constructs objects.

    As for learning about Perl objects: the information is spread across multiple documents, perltoot provides a better overview of how Perl handles key OOP concepts. perlobj is more of a discussion of how the Perl language implements objects, e.g. "A class is just a package...". In addition:

    • perlboot provides an Perl oriented introduction to the whole idea of OOP.
    • perltooc provides some options for managing class level data
    • perlbot provides some ways to get creative with objects: Perl has a very flexible approach - just about anything can be turned into an object: references to scalars, references to hashes, references to arrays, and even references to subroutines!

    Best, beth

Re: scope and packages
by Anonymous Monk on Jun 26, 2009 at 09:52 UTC
      oh, so that was just this stupid error... well, I guess next time I'll try to open my eyes even wider when reading the manual!
      Thx a lot for pointing that out!

        To be honest, you shouldn't need the manual for this. The constructor for an object should always be called using the arrow notation. Calling something with colons means that you're calling it as a plain subroutine; calling it with an arrow means you're calling it as a method.

        --

        See the Copyright notice on my home node.

        Perl training courses