Re: Passing data to XML::Twig twig_roots callbacks....?
"Tod Harter" <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
I'd say this is a perfect use case for currying...
somewhere in your code...
{
my $hashref = {}; # could also be blessed (ie an instance of a class or
tied)
my $callback = sub {
my ($twig,$element) = @_;
# do stuff. $hashref is now available for use in here, but private.
};
# now you can simply pass $callback as your callback function.
# any other code needing to refer to $hashref can be here.
}
On Tue, Dec 23, 2008 at 8:48 PM, Jenda Krynicky <[email protected]> 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
>
--
The Wise adapts himself to the world. The Fool adapts the world to himself.
Therefore, all progress depends on the Fool.
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs