Reinhard Pagitsch said:
> Thank you for your informations, it seems to me I have to do the same
> way. The most interesting things to me is where perl stores
> XS _return_ values and how  to capture it from the .pm module.
> e.g: I have a XS function  which  shall  return  a hash  to my .pm.

i've done that two ways (untested code follows, always doublecheck against the
perlxs and perlapi manpages):

  /* in an xs file */
  MODULE = Foo PACKAGE = Foo

  void
  return_a_hash ()
      PPCODE:
          /* return a list of key/val pairs that can be treated as
           * a hash by calling code. */
          EXTEND(SP, 4);
          PUSHs (sv_2mortal (newSVpv ("key_1", 5)));
          PUSHs (sv_2mortal (newSViv (3)));
          PUSHs (sv_2mortal (newSVpv ("key_2", 5)));
          PUSHs (sv_2mortal (newSVpv ("something", 9)));

  SV *
  return_a_hash_ref ()
      PREINIT:
          HV * hv;
      CODE:
          /* create a new hash, populate it, and return a reference to it. */
          hv = newHV ();
          hv_store (hv, "key_1", 5, newSViv (3), 0);
          hv_store (hv, "key_2", 5, newSVpv ("something", 9), 0);
          RETVAL = newRV_noinc ((SV*) hv);
      OUTPUT:
          RETVAL


  # in a pm file:
  %hash = Foo::return_a_hash ();  # actually a list of key/val pairs
  $hashref = Foo::return_a_hash_ref ();
  # $hash{key_1} == 3, etc
  # $hashref->{key_1} == 3, etc



-- 
muppet <scott at asofyet dot org>

Reply via email to