Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

overload and XS

by kmacleod (Initiate)
on May 18, 2001 at 06:37 UTC ( #81418=perlquestion: print w/replies, xml ) Need Help??

kmacleod has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use stringify overload on a tied XS variable (Perl 5.6.0) and I can't figure out how to turn on 'fallback'.

I was able to find SvAMAGIC_on() to turn on overload, and I provide a stringify method ('""') that works when you do "$ref", but I need

  $ref eq "some string"
to work also, and that means setting fallback => 1, which I can't figure out how to do to my XS-tied variable. My tie'ing sequence looks roughly like this:
      HV *hash = newHV();
      SV *hashref = newRV_noinc((SV*)hash);
      SV *tie = newSViv((IV)value);
      SV *tieref = newRV_noinc(tie);

      sv_bless(tieref, hash_tie_stash);
      sv_bless(hashref, xml_attribute_stash);
      SvAMAGIC_on(hashref);
      hv_magic(hash, (GV *)tieref, 'P');
      SvREFCNT_dec(tieref);
      return hashref;

Devel::Peek isn't giving me a clue (afaics) as to what magic is different between my XS-tied var and a pure-Perl tied var with overload magic.

Any clues appreciated!

Replies are listed 'Best First'.
(tye)Re: overload and XS
by tye (Sage) on May 18, 2001 at 09:10 UTC

    Why rewrite your Perl code in C?? Just because you have some XS code doesn't mean you have to do every single thing for that module in XS code!

    Do in C, only the stuff that is easy in C. Use a wrapper subroutine written in Perl to do stuff like what you are trying above. The code you have above is the kind of code that breaks when a new version of Perl comes out (or doesn't work when someone tries to use it on an older version of Perl than the one you developed it on).

    I've done this quite a bit and have been extremely happy with the results. I find I can make modules with much nicer, Perlish interfaces than I see in other XS modules. Plus the code ends up being much easier to understand, maintain, enhance, and debug.

            - tye (but my friends call me "Tye")

      This module is a bridge between C code and Perl code (similar to Inline), the 'value' above is a C pointer to the structure that is tied, that I want to be able to stringify. 'xml_attribute_stash' is the Perl wrapper package for that particular class, which has 'use overload' with '""' and 'fallback' specified.

      I found that I needed to use SvAMAGIC_on to enable overload on that tied C-structure so that the wrapper would see it as overloaded, but 'fallback' still isn't enabled. I'm guessing there's deeper magic to turn on fallback, since the perl wrapper isn't.

        What I am saying is: Move all of that code out of the C part of the module/XS. Have the C part just return the 'value' as an IV or UV in an orinary Perl scalar and do all of the hash creation, taking of references, tieing, overloading, and magic in Perl code.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://81418]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-12-09 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?