Re: Passing data to XML::Twig twig_roots callbacks....?

Dustin Suchter <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
Yep, the problem was a minor syntactic error nearby in my code
(parts not included here). The last version I emailed, which I'll
include again here for completeness, works as expected:

sub get_grandchild_info {
  my hash_ref = shift();
  return sub {
    my ($twig, $element) = @_;
    #blah blah, do something print-y here
  }
}

my $xmltwigobj = XML::Twig->new(
     twig_roots => {
       'Information/Child/Grandchild' =>
       get_grandchild_info($self),
     }
);

The named get_grandchild_info routine executes and sets up my
closure on instantiation. The anonymous routine is then called as
expected by the Twig parser as many times as are necessary.

Thanks all-

Jenda Krynicky wrote:
> From: Dustin Suchter <[email protected]>
>> So this code runs, but obviously doesn't pass my data. It loops the
>> expected number of times, given the XML it is parsing:
>>
>> sub get_grandchild_info {
>>   my hash_ref = shift();
>>   return sub {
>>     my ($twig, $element) = @_;
>>     #blah blah, do something print-y here
>>   }
>> }
>>
>> my $xmltwigobj = XML::Twig->new(
>>      twig_roots => {
>>        'Information/Child/Grandchild' =>
>>        get_grandchild_info,
>>      }
>> );
>>
>> If I change the object instantiation code to what immediately
>> follows, my subroutine executes precisely 1 time only:
>>
>> sub get_grandchild_info {
>>   my hash_ref = shift();
>>   return sub {
>>     my ($twig, $element) = @_;
>>     #blah blah, do something print-y here
>>   }
>> }
>>
>> my $xmltwigobj = XML::Twig->new(
>>      twig_roots => {
>>        'Information/Child/Grandchild' =>
>>        get_grandchild_info(),
>>      }
>> );
>>
>> And finally this version does the same thing, executing just the
>> first time the subroutine is "instantiated" - a term I'll use
>> lightly since I believe this is technically a closure-utilizing
>> callback, not an object:
>>
>> sub get_grandchild_info {
>>   my hash_ref = shift();
>>   return sub {
>>     my ($twig, $element) = @_;
>>     #blah blah, do something print-y here
>>   }
>> }
>>
>> my $xmltwigobj = XML::Twig->new(
>>      twig_roots => {
>>        'Information/Child/Grandchild' =>
>>        get_grandchild_info($self),
>>      }
>> );
>>
>> I'm guessing there's something wrong with the prototype for my
>> subroutine so $smltwigobj isn't really connecting with
>> get_granchild_info when I change the number of args... the question
>> is (a) why? and (b) how to fix that?
> 
> Without the actual code I can't tell. The last version (after fixig 
> the syntax) should be right. The get_grandchild_info() should be 
> called just once (when the $xmltwigobj is being created), but the 
> closure returned by that subroutine should be called several times.
> 
> Please create a complete script and example XML. 
> 
> Jenda
> ===== [email protected] === http://Jenda.Krynicky.cz =====
> When it comes to wine, women and song, wizards are allowed 
> to get drunk and croon as much as they like.
> 	-- Terry Pratchett in Sourcery
> 
> _______________________________________________
> Perl-XML mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.