Download Perl Script build rule
To add a build rule to visual studio, right click on your project and select 'Custom Build Rules...'

In the next dialog do the following:
1. Click Find existing and locate the rules file to add.
2. Check the box next to the rule to enable it.

That's it. By default the new rule will run before any files are compiled so if you are generating source files it should all work out ok. To prevent source files from always being marked as modified I create a test file in the intermediate directory to see when the target file was last changed. This only really works for me because I do want to monitor changes in the generated header file.