It depends what you exactly want to achieve and by what cost. If you want to prevent accidentally commits of files being a result of compilation, just add a .gitignore file excluding them from being committed (btw, this is always a good idea, exclude also backup copies and editor temporary files) and in a hook check if extension of committed file is in allowed list.
The aforementioned -T/-B check in Perl is nice, however, it's good to read proposed documentation. It's less efficient than checking the extension, but it gives the answer using the real content of the file.
If the -B/-T heuristic, as described in documentation, does not suit your needs, use file. In Perl you have few packages already available:
File::Type
File::LibMagic
File::MMagic
You will receive the MIME type of the file and you need to write some logic to interpret the result.
Unless there are no specific requirements which we are not aware of, I personally would stuck to .gitignore and checking extensions. Optionally, you can consider some kind of stick (preferably virtual, as corporal punishment is generally frowned upon) for developers committing forbidden files into repository.