There's a conceptual flaw in your sub. The splice would increase the number of elements in the array, but your alen variable isn't going up, so there will be a number of elements you never get to.

Let's start by translating your function into a pure Perl version that does what you want. I'm going to ignore package considerations for the present.

sub contentToString { my $list = shift; my $result = ''; if (ref($list) eq 'ARRAY' and @$list) { my $i; while ($i < $#$list) { my $list_entry = $list[$i]; if (ref($list_entry)) { if (ref($list_entry) eq 'ARRAY') { splice(@list, $i, 0, @$list_entry); } elsif (ref($list_entry) eq 'HASH') { $output_string .= tagToString($list_entry); } } else { $result .= $list_entry; } $i++; } } return $result . "\n"; }
1) If nth element of list is an arrayref $r, replace $r in the original list with @$r.

Is there a possibility that the data structure will be more than two levels deep? If so, you'll need a recursive function. UPDATE: Oh wait, as I look at the Perl function again, I see that it will work, and you don't need recursion.

2) If nth element of list is an hashref $r, append result of perl function F($r) to the result scalar string.

You're going to need to take a look at the perlcall docs in order to figure out how to make callbacks from XS to Perl work. It's a little complicated because you need to put in some stack manipulation stuff that xsubpp normally does behind the scenes for your XSUB.

--
Marvin Humphrey
Rectangular Research ― http://www.rectangular.com

In reply to Re: XS: Converting a data structure in a string by creamygoodness
in thread XS: Converting a data structure in a string by cosimo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.