Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:01:53+00:00 2026-06-16T19:01:53+00:00

Question How can I support autovivified filehandle arguments in an XS function? I’m XS-wrapping

  • 0

Question

How can I support autovivified filehandle arguments in an XS function?

I’m XS-wrapping a C function which returns a file descriptor, and I’d like to present that file descriptor as a perl filehandle argument in the manner of open(). E.g.,

myfunc(my $fh) or die "Error: $!";
do_something_with_fh($fh);

What I’ve done so far

Right now I’m using a perl wrapper on top of the XS function:

# -- in perl
sub myfunc {
    my $fd = _myfunc();
    return open($_[0], '+<&=', $fd) if defined($fd);
}

/* -- in XS */
SysRet
_myfunc()
    CODE:
    RETVAL = some_c_function_returning_an_fd();

    OUTPUT:
    RETVAL

This works Just Fine ™, but, again, I’d like to move the implementation entirely into XS.

So far I’ve tried sv_2io on an argument typemapped as SV *, but that throws an exception on undefined scalars. I have not tried mapping the first argument to a FILE * or PerlIO * object, since I don’t know how I’d “fdreopen” (if you will) those objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:01:54+00:00Added an answer on June 16, 2026 at 7:01 pm

    I would keep the myfunc() wrapper in Perl, it works and shouldn’t be a bottleneck.

    Reimplementing open() is tricky and requires usage of undocumented/internal API. I think this is a pretty close implementation. newGVgen() and do_openn() is part of the public API but undocumented and subject to change.

    void
    myfunc(sv)
        SV *sv
      PPCODE:
        {
            GV *gv = newGVgen("Mypackage");
            SV *rv = sv_2mortal(newRV_noinc((SV *)gv));
            SV *fd = sv_2mortal(newSViv(some_c_function_returning_an_fd()));
    
            if (!do_openn(gv, "+<&=", 4, FALSE, 0, 0, NULL, &fd, 1))
                croak("Could not fdopen descriptor: '%s'", Strerror(errno)); /* or XSRETURN_NO; */
    
            sv_setsv(sv, rv);
            SvSETMAGIC(sv);
            XSRETURN_YES;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question, how i can make application which can support all densities and
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
My original question can be found here , for which I've gotten some great
My Question: Can I support an older api and use functions of a newer
I am wondering about GAE possibilities and multi-client support; so the question is can
Question Can I style just a part of a single character? Meaning CSS attributes
Short Question: Can I specify wildcards for custom console folding? If so, what is
My question can be boiled down to, where does the string returned from stringstream.str().c_str()
Short question: Can anyone tell me what the requirements (especially when it comes to
I know this question can be answered by searching in google. But I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.