Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Perl_call_pv(, , G_ARRAY) returns references as integer type SVt_IV

by rogue_tache (Novice)
on Oct 01, 2012 at 14:26 UTC ( #996668=perlquestion: print w/replies, xml ) Need Help??
rogue_tache has asked for the wisdom of the Perl Monks concerning the following question:

Platform: Perl >= 5.14.x, AMD64, linux, ubuntu 12.04

I have some embedded perl code which has worked for years using a persistent interpreter with template code from the perlembed docs.

I call a perl function from C which returns a list of assorted perl types:

sub TestSimpleScalars { my $r1 = 40 + 2; my $r2 = 3.14159; my $r3 = "Humpty Dumpty"; my $r4 = [10, 20, 30]; my $r5 = { name => 'Fred', dateOfBirth => '13-Jan-1969' }; return($r1, $r2, undef, $r3, $r4, $r5, 'end list'); }

The C code uses Perl_call_pv(, , G_ARRAY) to ensure the perl code is evaluated in an array context. It then tests the type of each returned item in the list using SvTYPE(). The arrayref and hashref items used to correctly return a type of SVt_PVAV/SVt_PVHV, but now all references simply return the integer type SVt_IV.

Thus whereas I used to get types:

I now get:

It is as if the perl refs are being interpreted in a scalar context (the value that comes back is the internal ref integer value). This means it is impossible to return complex data structure from perl back to C.

This worked up to 5.10.1 This no longer works on v5.12 upwards (I tested 5.12.4, 5.14.2, 5.16.1)

If anyone has some example code showing call of a perl function that returns a complex data object with arrayref/hashref I would appreciate an example. Maybe I have just missed some magic flag somewhere?

Replies are listed 'Best First'.
Re: Perl_call_pv(, , G_ARRAY) returns references as integer type SVt_IV
by dave_the_m (Prior) on Oct 01, 2012 at 15:00 UTC
    SvTYPE() merely tells you the type of the storage container; it doesn't in general say what the logical type of the SV is.

    Since 5.12.0, the SVt_RV sv type has been abolished, on the grounds that the SVt_IV container type is perfectly capable of holding a reference.

    In general you shouldn't be basing code on tests like SvTYPE(sv) == SVt_RV; instead you should be testing the relevant flags, e.g.:

    if (SvROK(sv)) ref = SvRV(sv);


      Wonderful, that has sorted it. I am much obliged.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996668]
Approved by Corion
Front-paged by davido
[ovedpo15]: I need to use FindBin thought. does FindBin finds only modules? or it can find perl script also?
[moritz]: maybe you should start looking at the documentation of FindBin?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2018-03-17 20:51 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (226 votes). Check out past polls.