Hi Monks.

I have a bit of a problem with some XS code I'm writing.

I have a constructor "method" that creates a C struct and returns a reference to it:

VOX_PARSER *VOX_new_parser(void);

Here's the XSUB for it:

VOX_PARSER * new(CLASS) char * CLASS CODE: RETVAL = VOX_new_parser(); OUTPUT: RETVAL

And then in the typemap I map VOX_PARSER * to an O_OBJECT, and specify this for its OUTPUT filter:

# The Perl object is blessed into 'CLASS', which should be a # char* having the name of the package for the blessing. O_OBJECT sv_setref_pv($arg, (char *) CLASS, (void *) $var);

The problem is that another function calls that one to get the struct, uses it, and then returns it, and so does its XSUB. Consequently, the OUTPUT filter that should have only been applied once ends up getting applied twice, and a fatal error about the nonexistent "CLASS" variable (since the calling XSUB isn't a constructor, it doesn't take that argument like the new() XSUB does) gets raised when compiling. What would be the best way to take care of this? Should I stuff the filter code into the CODE portion of the new() XSUB? If so, what should I leave as the OUTPUT filter in the typemap?

There are at least a half a dozen other structs ("objects") that share this same problem: one function creates and returns, another calls, uses, returns.

Any advice would be appreciated. Thanks.


In reply to XSubs and returning blessed references? by Anonymous Monk

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.