For some reason my reply didn't post. The problem with this approach is that LyX expects the very first non-hash-commented line to be "\lyxformat 474" (or some other number). The only lines that I can modify without breaking LyX's ability to load the document are the #-comment lines, so I need some way to instruct Perl to do something within that line. Thus, #! fun. :-)