Adding 3 Lines to Multiple c and h files with perl

Hello, i need some help with a perl script.

i need to add the lines:

#ifdef LOGALLOC
#include "logalloc.h"
#endif // LOGALLOC

To all c and h files in a project with subdirectories.

Logalloc is a tool to log all *alloc and free's in a text file, it defines the *alloc funtions new.
#define malloc(size) logmalloc(size,__FILE__,__LINE__)

anyone fluent in perl could give me some hints?

thanks in advance
Lazzar

well i have it almost but arrgh

  1. with perl
    perl -pi -e "s:^:#ifdef MALLOC_TRACE\n#include <logalloc.h>\n#endif\n:" *.test

almost right but it replaces every begining of a line(^) with my string, it only needs to replace the 1st line!

  1. with sed
    sed -e "1s:^:#ifdef MALLOC_TRACE\n#include <logalloc.h>\n#endif\n:" *.test

sed replaces only the first line (notice the 1 infront of the 's'. (the perl script dosent support the 1)
But sed dosent recognize the newlines, i dont know how to define them in the cshell.

2 questions:
----------------
1.how do i declare a \n (newline) in the cshell?

  1. how can i limit my perl script to only one (the first) line?

Lazzar

ok got sed working but i'd love to do it with perl, cause i think it would be alot faster.

sed -f script *.c

script:
1s:^:#ifdef MALLOC_TRACE\
#include <logalloc.h>\
#endif\
:

it replaces the beginning of the first line of all *.c files with my 3 ifdef lines.

i still need help with the perl script tho

Lazzar