Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to call Encode::decode from Perl XS

by mje (Curate)
on May 09, 2011 at 08:36 UTC ( [id://903729]=perlquestion: print w/replies, xml ) Need Help??

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

I am looking at the RT DBD::ODBC doesn't handle windows-1252 characters correctly. The only place I can decode data from the database (without adding a fetch method just for this) is by calling Encode::decode from the XS code. Anyone know if you can do this and how?

  • Comment on How to call Encode::decode from Perl XS

Replies are listed 'Best First'.
Re: How to call Encode::decode from Perl XS
by Anonymous Monk on May 09, 2011 at 09:00 UTC

        That code uses sv_utf8_decode not Encode::decode. I already use sv_utf8_decode.

      Thanks, callback functions looks useful and probably what I was looking for.

Re: How to call Encode::decode from Perl XS
by Tux (Canon) on May 09, 2011 at 13:48 UTC

    Bases on my experience, it could be something like (untested)

    int encode_loaded = 0; SV *m_encode = newSVpvs ("decode"); /* Or "Encode::decode", I'm not su +re */ SV *sv_enc = newSVpvs ("cp1252"); my_foo (char *buffer) { int result; if (!encode_loaded) { ENTER; load_module (PERL_LOADMOD_NOIMPORT, newSVpvs ("Encode"), NULL, + NULL, NULL); LEAVE; encode_loaded = 1; } PUSHMARK (sp); EXTEND (sp, 2); PUSHs (sv_enc); PUSHs (newSVpv (buffer)); PUTBACK; result = call_sv (m_encode, G_SCALAR | G_METHOD); SPAGAIN; if (result) { SV *encoded = POPs; /* Do more */ } PUTBACK; }

    And some mortalization might be needed to not leak.


    Enjoy, Have FUN! H.Merijn
      all that load checking goes in the .pm file in the form of "use Encode...", kiss :)

        Yes and no. If you want a module to automatically load another module from XS, and not import all functions/methods, this is a cleaner way to do so. Of course you can croak/fail/die/barf/puke when a user tries to invoke something that he/she did not explicitely load, but as Encode is a CORE module I see no harm in hiding the require from XS and making the underlying module DWIM more.

        I agree that this is a grey/gray area and opinions may well differ if which case I think you should agree to disagree.

        XS is a different world and pulling the right strings is sometimes extremely hard compared to how easy perl made it in the language level itself.


        Enjoy, Have FUN! H.Merijn

      Thanks Merijn, I'll look into this.

Re: How to call Encode::decode from Perl XS
by anonymized user 468275 (Curate) on May 09, 2011 at 09:01 UTC
    If the target database is MS SQL-Server, then perhaps DBD::Sybase would be able to handle it more transparently. It's built on top of a Sybase Open Client library that is specifically designed for Sybase and MS SQL Server is pretty much an MS porting of the Sybase Enterprise Server and afaik still relies on Sybase Open Client for its connectivity.

    One world, one people

      The rt reporter was using MS SQL Server. See automatic character encoding handling in perl dbi dbdodbc. However, he is also using DBD::ODBC. I've maintained DBD::ODBC for a lot of years and no one has ever reported this to me but I already do some UTF-8 decoding and thought it would be easy to add. There are other issues moving to DBD::Sybase but I don't want to start an argument here.

        Well, I am not intending any module bias here. But what occurred to me in fact was that Sybase Open Client reads locales.dat and uses it to pass the client character set at login time to SQL Server. I was wondering whether OBDC, being a database independent architecture with a standardised driver interface was bypassing that Sybase-specific step.

        One world, one people

      Just a small note - when using DBD::Sybase with MS-SQL you're not using Open Client (or at least not the Sybase Open Client implementation).

      In that situation you use FreeTDS, which I believe uses libiconv for char conversions.

      Michael

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found