Perl and C are my two languages of choice at the moment. (I intend to add Haskell at some point, in my Copious Free Time.) So naturally, sometimes I find myself writing C that looks like this:

for($i = 0; $i < $n; $i++) { &foobar($arr[$i]); }
No big surprise there. But today at work, hacking together another DBI script, I wrote:
#! /usr/bin/perl -w use struct;
Naturally, struct wasn't what I meant to type. But my instinctive reaction to the error message was rather unusual, and (in retrospect) quite amusing:
#! /usr/bin/perl -w typedef struct;
Whoops. Anyways, I figured I'd give you fine folks a chance to have a chuckle at my expense. Anyone else have similar stories that they wouldn't mind sharing?

Blessed be

Update: Gah. Well, it was Samhain in localtime when I posted.

--
:wq

Replies are listed 'Best First'.
Re: Perl and C don't always mix well
by japhy (Canon) on Nov 01, 2001 at 08:15 UTC
    For those of you who can't get enough of Perl...
    #define elsif else if #define unless(x) if (!(x)) #define $ #define @ #define %

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Ick.

      But not as far from reality as you may imagine. For those of you who've never seen the code to the original Bourne shell, S. R. Bourne had a bunch of macros defined like this:

      #define BEGIN { #define END } #define IF if( #define THEN ){ #define ELSE }else{ #define FI } #define LOOP for(;;){ #define POOL }
      And so on. The code looked like a funky variant of Algol-68, or C written on heavy psychotropic drugs.

      I once had to debug the code when we were porting it. The only way I could cope was to run it through the C-preprocessor and awk the result. (Why not run it through Perl, you ask? It didn't exist yet....)

(jeffa) Re: Perl and C don't always mix well
by jeffa (Bishop) on Nov 01, 2001 at 08:10 UTC
    Going back to Java one day, i found myself trying to check for the equality of a string like so:
    if (foo eq "bar") . . .
    Then i realized, "Hang on! Java doesn't have eq, it only has ==" . . . So:
    if (foo == "bar") . . .
    And wondered for a long amount time (much longer than i care to admit) why it was always, always, always false . . .

    jeffa


    Answer:

    Because i was comparing address spaces. The proper way to compare strings in Java is with the equals() method from the String class.

      Random tip that works well across several languages.

      Turn that test around. If you write it:

      if (5 == $number) { # ... }
      then you are automatically told your mistake when you write
      if (5 = $number) { # ... }
      by accident. By contrast you are rather less happy if you write in the naive order and wound up with:
      if ($number = 5) { # ... }
      which winds up being true rather more often than you would like...
Re: Perl and C don't always mix well
by kwoff (Friar) on Nov 01, 2001 at 08:04 UTC
    Yep, I put in the '$' especially, use 'my' for declaring, and put '#' for comments. It's just a habit with my fingers.
    int main () { # my $foo; }
    But that's only when I haven't programmed in C for a while.
Re: Perl and C don't always mix well
by Zaxo (Archbishop) on Nov 01, 2001 at 09:54 UTC
    # define an array of signal handlers # include a human reaadable description # if SYMBOL is not not defined ...

    After Compline,
    Zaxo

Re: Perl and C don't always mix well
by Sweeper (Pilgrim) on Nov 02, 2001 at 11:33 UTC
    It reminds me that, a few months ago, I wanted to write an "Hello world" program in C. It took me three compiles before executing it. The successive attempts were:
    #include <stdlib.h> #include <stdio.h> void main () { printf "Hello world\n" }
    Oops, forgot that you need a semi-colon, even if followed by a closing brace.
    #include <stdlib.h> #include <stdio.h> void main () { printf "Hello world\n"; }
    What's wrong now? (time passes...) Ah yes, you need parentheses.
    #include <stdlib.h> #include <stdio.h> void main () { printf("Hello world\n"); }
    Yeah, it works!