Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Yet Another XS Tutorial

by ikegami (Pope)
on Nov 20, 2013 at 15:22 UTC ( #1063543=note: print w/ replies, xml ) Need Help??


in reply to Yet Another XS Tutorial

Problems:

  • Your code won't always compile. PPCODE cannot start with variable declarations as it can be preceded by non-declarations.

    PPCODE: int i; f();
    should be
    PREINIT: int i; PPCODE: f();
    or
    PPCODE: { int i; f(); }
  • Your snippets don't handle magical variables (e.g. $1, %ENV) except sometimes by chance. e.g.

    say lengths1(substr("abc", 0, 1)); # 0
  • lengths1 uses SvLEN when it should be usingSvCUR. e.g.

    say lengths1("abc"); # 16
  • Your snippets suffer from The Unicode bug. e.g. After fixing the above problems,

    $_="\x{A0}"; say lengths1($_), "=", length($_); # 1=1 $_="\x{A0}\x{2660}"; chop; say lengths1($_), "=", length($_); # 2=1
  • lengths1 uses type-based polymorphism —something which should be avoided in Perl— and does it "poorly". e.g. After fixing the above problems,

    say lengths1(456), "=", length(456); # 0=3


Comment on Re: Yet Another XS Tutorial
Select or Download Code
Re^2: Yet Another XS Tutorial
by cdybedahl (Acolyte) on Dec 14, 2013 at 11:34 UTC
    As you may have seen from the introductory text, the whole text is written while learning XS. Given that, pointing out problems with it as "You have the florbleblitz problem" is not terribly useful. It would be far more useful if you could provide explanations of what the problem is, why it is a problem and ideally some hints on how to fix it. Or, of course, links to existing texts with such information. This goes extra much for anything to do with magic, since the documentation included with Perl on that subject explicitly says that it's obsolete and should not be used.

      It would be far more useful if you could provide explanations of what the problem is, why it is a problem

      Each problem came with code to demonstrates it.

      and ideally some hints on how to fix it.

      Magic: Use SvGETMAGIC(sv) before reading from an SV. Use SvSETMAGIC(sv) after writing to an SV.

      Unicode bug: Just like numbers can be stored in a scalar in a number of ways, so can strings. Accessing a string's buffer without determining how data is stored in it is a bug. (This pretty much rules out using `char *` in XS function prototypes.) If you expect bytes, SvPVbyte will get them. If you expect text, SvPVutf8 will get it encoded using utf8.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063543]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-19 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (138 votes), past polls