Don't reinvent the wheel; there are traps in tryign to do this yourself from scratch. I swear I found SLOC in a cpan module once, but I can't seem to find it now. If I come up with a productive search term I'll re-reply.
update: Text::Diff
Here's another free tool that promises alot, though I haven't tried it myself:
SLOCCount
In reply to Re: SLOC script to report Number of lines Added, deleted & modified
by goibhniu
in thread SLOC script to report Number of lines Added, deleted & modified
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |