in reply to Re: XML::Parser Object Orientation problem
in thread XML::Parser Object Orientation problem

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:
First the ShimPractise.pm module:
#!/perl/bin/perl #Practise shim form of xml parsing. package ShimPractise; use strict; use XML::Parser; sub new ($$$) { my $shimClass = $_[0]; my $shimData = { name => $_[1], number => $_[2], xmlAsScalar => $_[3], }; bless ($shimData, $shimClass); return $shimData; } sub parse() { my ($currentObject) = @_; my $parser = new XMLParser(Handlers => { Init => sub {$currentObject->handleDocumentInit (@_)}, Start => sub {$currentObject->handleStartTag (@_)},}); $parser->parse($currentObject->{xmlAsScalar}); my $returnString = ""; my $numTags = $currentObject->getNumber(); $returnString .= "Number of Tags: ".$numTags."<BR>\n"; my $tags = $currentObject->getName(); $returnString .= $tags."<BR>\n"; return $returnString; } sub setName($) { $_[0]->{name} = $_[1]; } sub setNumber($) { $_[0]->{number} = $_[1]; } sub getName() { return $_[0]->{name}; } sub getNumber() { return $_[0]->{number}; } sub handleDocumentInit() { my ($currentObject, $expat) = @_; $currentObject->setName("Tags: "); $currentObject->setNumber(0); } sub handleStartTag() { my ($currentObject, $expat, $element, %attrList) = @_; my $currentName = $currentObject->getName(); my $currentNumber = $currentObject->getNumber(); $currentObject->setName($currentName." ".$element); $currentObject->setNumber($currentNumber + 1); } 1;
And now the code I use to call it:
my $shimmer = new ShimPractise("name", 0, "<Tag1><Tag2></Tag2><Tag3> +</Tag3></Tag1>"); my $shimString = $shimmer->parse();


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

Replies are listed 'Best First'.
Re^3: XML::Parser Object Orientation problem
by GrandFather (Saint) on Sep 08, 2009 at 00:38 UTC

    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