Need to learn C, best books?
Andy Armstrong
n at rciss.us
Wed Oct 24 14:07:30 BST 2007
On 24 Oct 2007, at 13:28, Lyle - CosmicPerl.com wrote:
> Hi All,
> The more I work with Perl modules that use a lot of C, the more I
> find the need to learn C.
> I've seen the Perl for C programmers book, but I want a C for Perl
> programmers book.
>
> Is there a C programming book equivalent to "Learning Perl" by
> Randal? I've read several Perl books, but when I finally picked up
> a copy of Learning Perl is was obvious to me that I had wasted time
> on the other Perl introductory books.
Perl for C is more or less just C. Since so much of what Perl does
simply isn't natively available in C there's not much scope for
showing C and Perl equivalents side by side. IMO anyway.
> Also is there a C equivalent to PerlMonks and PerlM[ou]ngers?
Don't think so. comp.lang.c used to be the wellspring of all esoteric
C knowledge but it's years since I've looked at it.
The main pain for a Perl programmer will be understanding the extent
of your responsibility for memory management and coming to terms with
the potential for shooting yourself in the foot. Just because a
program appears to be working doesn't mean that you're not doing
things like reading beyond the end of an allocated block. It's quite
possible, for example, to trample on a local variable in a way that
isn't apparent until you change something later and start getting
what seem to be random crashes. No safety net.
Once you have your head properly around that kind of thing
interfacing with Perl is pretty simple. Investigate Inline::C - it's
easier than plain old XS.
> Honestly the last C I did was about 10 years ago, I didn't to much
> more than keyboard/mouse input, file opens & saves, and a load of
> getpixel and setpixel routines to make a little game (did also do
> some crappy AI). Can't really remember any of it. Since I started
> doing Perl I never really looked back at C or VB...
One of my hobby horses is that *everyone* should work with more than
one language. I consider it pretty much essential - particularly for
anyone who cares about continuously improving their practice.
A couple of things to bear in mind if you're hoping to speed your
Perl programs up with bits of C:
* thunking between Perl and C is not cost free. If you're calling
some C that doesn't take long to execute any speed up is lightly to
be swamped by the cost of marshalling args and return values between
Perl and C.
* Perl gives you access to a lot of optimised C already - it's built
into the interpreter. You may find that, e.g., an ad-hoc string
scanning routine written in C is slower than Perl.
Look at this:
/* somestring is 1MB of data */
for (i = 0; i < strlen(somestring); i++) {
if (somestring[i] == '!') {
return i; /* found the needle */
}
}
That might look superficially innocent enough - and for short strings
you won't notice any problems - but it's O(N^2) where N is the length
of the string. Traps like that abound.
--
Andy Armstrong, Hexten
More information about the london.pm
mailing list