How to return list from Inline::C code?

Discussion in 'Perl Misc' started by kj, Jul 29, 2005.

  1. kj

    kj Guest

    I'm attempting to hack the Perl internals, something I know very
    little of. So I apologize ahead of time for the cluelessness of
    this question.

    I want to implement a function via Inline::C such that, depending
    on context, it returns either a scalar or a list of two values.
    (This function will access the internals of other variables, and I
    want to make it as fast as possible, hence the decision to code it
    in C).

    Since C is not capable of optionally returning a scalar or a list,
    I don't even know where to begin with this.

    Any advice would be much appreciated. In particular, I'd love to
    look at source code for a simple function (in C) that returns a
    scalar or a list according to calling context.

    Thanks!

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 29, 2005
    #1
    1. Advertising

  2. kj

    Sisyphus Guest

    "kj" <> wrote in message
    news:dce677$blb$...
    >
    >
    >
    > I'm attempting to hack the Perl internals, something I know very
    > little of. So I apologize ahead of time for the cluelessness of
    > this question.
    >
    > I want to implement a function via Inline::C such that, depending
    > on context, it returns either a scalar or a list of two values.
    > (This function will access the internals of other variables, and I
    > want to make it as fast as possible, hence the decision to code it
    > in C).
    >
    > Since C is not capable of optionally returning a scalar or a list,
    > I don't even know where to begin with this.
    >
    > Any advice would be much appreciated. In particular, I'd love to
    > look at source code for a simple function (in C) that returns a
    > scalar or a list according to calling context.
    >


    As William Ahern pointed out, GIMME_V seems to be your friend here:

    D:\pscrpt\inline\special>type gimme_v.pl

    use warnings;
    use Inline C => Config =>
    BUILD_NOISY => 1;

    use Inline C => <<'EOC';

    void foo() {
    Inline_Stack_Vars;

    if(GIMME_V == G_SCALAR) {
    printf("%s\n", "return scalar");
    Inline_Stack_Reset;
    Inline_Stack_Push(sv_2mortal(newSViv(42)));
    Inline_Stack_Done;
    Inline_Stack_Return(1);
    }

    if(GIMME_V == G_ARRAY) {
    printf("%s\n", "return array");
    Inline_Stack_Reset;
    Inline_Stack_Push(sv_2mortal(newSViv(65537)));
    Inline_Stack_Push(sv_2mortal(newSViv(65539)));
    Inline_Stack_Done;
    Inline_Stack_Return(2);
    }

    if(GIMME_V == G_VOID) {
    printf("Nothing to do ... \n");
    Inline_Stack_Void;
    }

    }



    EOC

    $x = foo();
    print $x, "\n\n";

    @y = foo();
    print "@y\n\n";

    foo();

    __END__
    D:\pscrpt\inline\special>perl gimme_v.pl
    return scalar
    42

    return array
    65537 65539

    Nothing to do ...

    Cheers,
    Rob
    Sisyphus, Jul 30, 2005
    #2
    1. Advertising

  3. kj

    kj Guest

    Many thanks for all the replies! I'm in business now.

    kj

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Jul 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    728
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    466
    E. Robert Tisdale
    May 6, 2005
  3. Replies:
    3
    Views:
    451
  4. Daniel Vallstrom
    Replies:
    2
    Views:
    1,833
    Kevin Bracey
    Nov 21, 2003
  5. Nish
    Replies:
    4
    Views:
    480
    Thomas Stegen
    Oct 8, 2004
Loading...

Share This Page