in reply to Callback function in xsubpp

Calling keys in your Perl sub resets the iterator (see each) in your XS code.

- tye        

Replies are listed 'Best First'.
Re^2: Callback function in xsubpp (each)
by sriniiyer (Initiate) on Jul 31, 2013 at 05:03 UTC
    Thank you for the reply. So, is there any way to retain the iterator position? Or, should I always copy the hash into another hash and then call the subroutine?

      What I would do is honor my best practice of not manipulating Perl data structures from XS code and just iterate over the keys in regular Perl code using for my $key ( keys %hash ) [which makes a copy of just the keys].

      - tye        

        This is what I did to solve the problem. Firstly, I took all the keys in the hash and pushed them into an array defined using AV*. Then, I iterated over this array by popping the topmost element during every loop iteration. In parallel, I updated the hash with the results and executed the callback function too.

        Using the array instead of keys ensured that the iterator does not get reset. Also, the array would be empty when I exit the function.

        Thank you Tye for pointing out the mistake that I had done in the program.

        Completely agree with you. But, my XS function is going to run for long time (sometimes more than a day). At this point of time, I will be calling the Storable::store() in the callback routine to save the intermediate status. This will help me in checking the progress, whenever I need to. Would be happy to know if you have any better solution for this.