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

I find myself in an unusual situation. Usually we need to train C programmers in how to use this new fangled perl thing. But times have changed, and now I find myself with a class full of Perl programmers, many of whom have never used a C compiler before. I need to give them enough know-how to maintain XS code.

So now instead of having to explain how Perl automatically handles types and memory management, I need to explain that C doesn't handle types or memory management. And what a type is. And what memory management is.

All of my google searches return resources for C to Perl, I can't find anything on Perl to C. I'm looking at adapting a few simple "Intro to C" talks, but I'd like to take advantage of the similarities between Perl and C to speed this up.

How would you go about this?

Replies are listed 'Best First'.
Re: C tutorial for Perl programmers?
by DrHyde (Prior) on Sep 03, 2013 at 10:18 UTC

    There are two good sources from which one can learn C. I recommend using both.

    The first is K&R. The second is Learn C The Hard Way. I recommend using both to learn the language - the first is beautifully concise, the second has lots of handy tips and more examples.

    You will need the second to learn the toolchain. K&R hardly (at all? I disremember) mentions 'make' or tells you how to link. LCTHW has whole chapters on them, as well as chapters on testing and debugging.

      LCTHW looks perfect, thanks for the tip. I might be able to adapt it for Perl fairly quickly. Starting with doing a search and delete for every instance of "stupid", "asshole" and "women should stop whining".

        Starting with doing a search and delete for every instance of "stupid", "asshole" and "women should stop whining".

        Que?

Re: C tutorial for Perl programmers?
by bulk88 (Priest) on Sep 03, 2013 at 05:55 UTC
    Teach strict types, tell them types are objects. Teach casts. Teach machine representation/memory/pointers. Teach memory management (malloc vs static vs alloca vs auto). Teach what .objs and .libs are. Teach symbol resolution/linking. Teach them "(*(__int64 **)&pointer) += 2" does not add 2 to the pointer but 16. Named parameters. Type promotion for extra credit. "if" doesn't need curlies if 1 line long. "switch". No sigils. Thats all I can think of. Both Perl and C are all algebra and PEMDAS.
      That looks like a good start. I might leave pointer arithmetic for "if we have more time". I could probably spend a few days on that alone, likewise type promotion.

        Also, don’t overlook these:

        • definition vs. declaration
        • the preprocessor: #define, conditional compilation, #include
        • arrays and const
        • struct, union, enum, and typedef
        • the more commonly-used standard libraries: stddef.h, stdio.h, string.h, stdlib.h
        • the different uses of static

        Some more advanced topics for later:

        • extern
        • variadic functions
        • long jumps

        Also of practical help when teaching memory management: explain the meaning of “segmentation fault” and “bus error” — your student(s) will be seeing a lot of these! ;-)

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Dont leave pointer arithmetic for later. Teach it early. It was the hardest thing for me to understand when going from Perl to C, if a pointer is just a number, why is +2 way to far off to the right in memory? Do I have to do +0.5 to get to the first slice? (no, go learn pointer arthmitic)
Re: C tutorial for Perl programmers? (hard lint strict warnings bondage perlxspp)
by Anonymous Monk on Sep 03, 2013 at 08:30 UTC
Re: C tutorial for Perl programmers?
by syphilis (Archbishop) on Sep 03, 2013 at 10:02 UTC
    I need to give them enough know-how to main XS code

    I guess the first thing they need to know is the basics of C - and the fact that they already know some perl is pretty much irrelevant, IMO.
    That is, I think they need to be taught the C basics the same way that someone who has never programmed at all is taught the basics of C. Since these people have already programmed in perl, they may well learn more quickly than those who have not done any programming at all, but I think both groups should be given the same course (whatever that may be).

    Once they have some proficiency in C, they then need to be taught how to implement C code in perl. For this, two things come to mind:
    1) the perlxstut, perlxs and perlguts documentation;
    2) the demos in Inline::C's C-Cookbook.pod

    In the final analysis, there's not much difference between XS and Inline::C - and I think the latter's capacity to help programmers learn the former should not be underestimated.

    For someone who knows perl but not C, it's a fairly steep learning curve - a curve that I would not expect to be negotiated quickly.

    Cheers,
    Rob

      I had been hoping I could relate things to what they already know in Perl. e.g. they've all seen things like ARRAY(0x84902a0), the deference arrow works in a similar way in Perl and C, and a lot of the syntax is deliberately similar.

      I don't have a lot of hope for getting people up to XS, but I'd like to give them a good start.

        I don't have a lot of hope for getting people up to XS, but I'd like to give them a good start

        The nice thing about Inline::C is that it's XS under the hood but, to use it, you don't really need to know any XS at all - you just need to know some C (and, for starters, to read the perlapi documentation).

        Cheers,
        Rob
Re: C tutorial for Perl programmers?
by Mr. Muskrat (Canon) on Sep 04, 2013 at 21:44 UTC

    I've heard good things about "Understanding and Using C Pointers" by Richard Reese. In fact, I just ordered a copy.