I want to write a C subroutine alias(dest_package,dest_name,src_package,src_name) that implements something like *dest_package::dest_name = *src_package::src_name. I'm having a lot of trouble with it. I think I have the right thing when *dest_package::dest_name already exists, but I don't know how to create it (with the proper refcounts) if it doesn't.

Here's what I have now:

static void alias(char* dest_package, char* dest_name, char* src_package, char* src_name) { HV* src_stash = gv_stashpv(src_package, 0); SV* src_glob = *hv_fetch(src_stash, src_name, strlen(src_name), 0); HV* dest_stash = gv_stashpv(dest_package, 0); SV** dest_globP = hv_fetch(dest_stash, dest_name, strlen(dest_name), + 1); SV* dest_glob; if (dest_globP) { dest_glob = *dest_globP; } else { // WHAT GOES HERE? dest_glob = ...; } SvSetMagicSV(dest_glob, src_glob); }
(hopefully I didn't mess that up when modifying it for posting.)

I know I could do this by constructing the string "*dest_package::dest_name = *src_package::src_name" and passing it to eval_pv, and I may end up doing it that way, but for my own education if nothing else I'd love to know how to do this.

Oh, and are my final arguments to hv_fetch correct? I'm not sure when you should set the lval attribute to true.


I work for Reactrix Systems, and am willing to admit it.

In reply to implementing import in XS by sfink

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.