in reply to XML::Parser Object Orientation problem

The general trick to solve this issue is to use an anonymous sub as a shim. Consider:

Start => sub {$self->handleStartTag (@_)},

If you have extra parameters you want to pass to the handler you can include those before @_ (after works too if you can guarantee the length of @_, but that's generally not a safe thing to assume).


True laziness is hard work

Replies are listed 'Best First'.
Re^2: XML::Parser Object Orientation problem
by shug94 (Sexton) on Aug 30, 2009 at 02:53 UTC
    I have tried doing this in a little practise program, and I am still getting stuck.

    Please forgive my Perl newb-dom.

    Here is the error I am getting, and it doesn't really make too much sense to me, given that I am not constructing a new ShimPractise on that line:
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] Premature end of script headers: SBG_ReceivedMessageListener.pl
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] Not enough arguments for ShimPractise::new at ShimPractise.pm line 26, near ");"\r
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] Compilation failed in require at SBG_InitiateRetrieveLoggedEventDataRequestHandler.pm line 8.\r
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] BEGIN failed--compilation aborted at SBG_InitiateRetrieveLoggedEventDataRequestHandler.pm line 8.\r
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] Compilation failed in require at C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/SBG_ReceivedMessageListener.pl line 5.\r
    [Sun Aug 30 10:30:50 2009] [error] [client 127.0.0.1] BEGIN failed--compilation aborted at C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/SBG_ReceivedMessageListener.pl line 5.\r


    And here is the code:

    Any idea why this gives me such a confusing error message? Is the XML::Parser trying to construct a new instance of ShimPractise when it calls the handleStartTag function?

    Any help is much appreciated, cheers,
    Shug

      Don't use Perl prototype subs - they don't do what you think they do and they probably do things you don't expect.

      Calls to new should be of the form Package->new (...). You need to fix the calls to new for both ShimPractise and XML::Parser (which you wrote as XMLParser in the code btw).

      With those bugs fixed and print $shimString; appended your code generates:

      Number of Tags: 3<BR> Tags: Tag1 Tag2 Tag3<BR>

      True laziness is hard work