Hi,

Please excuse the bumbling ineptitude which typifies my (thankfully) rare submissions to this list. On the previous previous occasions I have asked questions, the answer has been embarrassingly straightforward. I am kind of hoping for the same this time. (Also please excuse if there is a disclaimer below, I have been working on circumventing it, hopefully I have succeeded).

I have been mucking about with XML quite a bit recently, and managed to
get XML::Parser to work quite nicely, using subroutines as event handlers. However I have a healthy dislike of global variables, and am
writing a module to fit into someone else's software, and would rather
keep things tidy.


In my reference book (Perl & XML by Ray & McIntosh)it clearly states that one can use object methods as handlers to avoid this global variable scenario. However, the book and the XML::Parser manpage fail to give specific examples as to how this is accomplished, and when I try I encounter problems.

It seems I can use a subroutine reference as the handler without any
parameters and get the expat object reference and the XML stuff like the element name or tag contents passed through to the handler e.g.


#----------------------------------------------------------------------



my $parser = new XML::Parser(   Handlers => {
        Start   => \&parse_start_field,
        Char    => \&parse_char_field,
     } ) ;

$parser->parse( $xml_object->{xml_string} ) ;

sub parse_start_field {
        my $expat = shift ;
        my $element_name = shift ;

                ... do stuff with $element_name but
                only to global variables because
                I have no means to handle whatever
                I return from this subroutine...

}

#----------------------------------------------------------------------

OR

I can pass my own parameters ( including the object reference in the case of object methods ) e.g.

#----------------------------------------------------------------------

my $parser = new XML::Parser(   Handlers => {
        Start   => $xml_object->parse_start_field(),
        Char    => $xml_object->parse_char_field(),
   } ) ;

$parser->parse( $xml_object->{xml_string} ) ;

sub parse_start_field {
        my $xml_object = shift ;        # this works
        my $expat = shift ;             # nothing in this variable
        my $element_name = shift ;      # nothing in this variable

                ... do stuff with $xml_object but
                nothing useful because I have no element
                name...

}

#----------------------------------------------------------------------


The problem with this second way of doing it is that I don't get the expat reference (which does not bother me) or any of the XML stuff
( which makes the whole thing pretty pointless ).


I have also tried:

Start   => \&parse_start_field($xml_object),
Start   => \&parse_start_field($xml_object, @_),
Start   => &parse_start_field($xml_object),
Start   => &parse_start_field($xml_object, @_),
Start   => parse_start_field($xml_object),
Start   => parse_start_field($xml_object, @_),

and quite a few other combinations based on a kind of masochistic perserverance rather than any technical reason.


So which is true ?


a) Messrs Ray and McIntosh are lying to me
b) I am missing something obvious
c) I am missing something non-obvious

If someone could put me out of my misery I would be grateful.


Thanks,


Rhys.






Reply via email to