perl source code

acme on 2004-02-02T11:09:10

Sometimes I wonder about the perl source code. So I was looking at what Perl_looks_like_number actually does. And the code is the following:

I32
Perl_looks_like_number(pTHX_ SV *sv)
{
    register char *sbegin;
    STRLEN len;

if (SvPOK(sv)) { sbegin = SvPVX(sv); len = SvCUR(sv); } else if (SvPOKp(sv)) sbegin = SvPV(sv, len); else return 1; /* Historic. Wrong? */ return grok_number(sbegin, len, NULL); }
Mmmm, funky backwards indenting and amusing comment with two returns in a row...


Blocks in C

davorg on 2004-02-02T12:17:07

with two returns in a row
Not quite. Single statement blocks in C don't need surrounding braces, so the two returns there do make sense. The first one is associated with the else branch. As the reverse indenting attempts to make clear :)

Re:Blocks in C

yDNA Barak on 2004-02-02T12:56:07

Wait a minute. Okay, I'm no C jockey, but doesn't this mean the final return statement could never be reached?
if (SvPOK(sv)) {
  sbegin = SvPVX(sv);
  len = SvCUR(sv);
}
else if (SvPOKp(sv))
  sbegin = SvPV(sv, len);
else
  return 1; /* Historic.  Wrong?  */

return grok_number(sbegin, len, NULL);
I've got my K&R right here, buddy! ;)

Re:Blocks in C

davorg on 2004-02-02T13:02:59

No. The "return grok_number" still gets called if either SvPOK or SvPOKp return a true value.

For example, if SvPOK returns true, then sbegin and len are set and the program execution moves to the first statement _after_ the if/else block, i.e. the "return grok_number".

It's been a loong time since I wrote C, and my memory could be fading, but I'm pretty sure I'm right here.

Re:Blocks in C

yDNA Barak on 2004-02-02T13:07:31

Ah, duh. Got it. Just like what I'd want perl to do. Good thing I don't mess with C much.

tabstops

bart on 2004-02-02T12:38:51

Did you happen to set your tabstops to 3 spaces?

It looks to me like the source uses tab characters to indent code, and your editor shows it as 3 spaces instead of the traditional 8.

Tabs must die.

schwern on 2004-02-03T00:41:04

That is all.