Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Re: XML::Parser Tutorial

by gildir (Pilgrim)
on Mar 07, 2001 at 21:40 UTC ( #62789=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: XML::Parser Tutorial
in thread XML::Parser Tutorial

Well, but .... (there is allways a 'but') :-)

Suppose I do not subclass XML::Parser. But then, how do I pass parameters to XML::Parser handler methods and collect results of their run without using global variables of XML::Parser package? Only class that I get to handler methods is expat itself and there is no place for any aditional parameters/results of handler methods.

And if I subclass XML::Parser, only advantage that I gain is using my own package namespace for global variables instead of XML::Parser's namespace. This do not looks to me like a good example of object oriented programming style.

Possible silution is the one mirod suggested using Non-Expat-Options but it is just a little bit less ugly than these two.

There best solution will be forcing XML::Parser to use my custom subclass of XML::Parser::Expat instead of XML::Parser::Expat itself. Is there some way how to do that?


Comment on Re: Re: Re: XML::Parser Tutorial
Re: Re: Re: Re: XML::Parser Tutorial
by Anonymous Monk on Mar 14, 2001 at 15:41 UTC
    The way to do this, without relying on the fact that the $p is a hashref, is to pass a closure as the handlers, and have an object that you created saved in the closure. This is how PerlSAX is implemented.

    Witness:

    my $handler = bless {}, "MyHandler";
    my $p = XML::Parser->new(Handlers => {
       Start => sub { $handler->handle_start(@_) } 
    });
    
    package MyHandler;
    
    sub handle_start {
      my ($handler, $p, $element, %attribs) = @_;
      ...
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://62789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2015-07-05 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls