When you use ! with vi, it sends the text to the program on STDIN, and replaces it with whatever the program outputs on STDOUT. Try something like this:
#!/usr/bin/perl -w
use strict;
while (<STDIN>) {
chomp;
if ( ! -e ) {
print "#$_\n";
} else {
print "$_\n";
}
}