The $ in the regex needs to be escaped (like this: s/ \$VAR1/ my %hash /) so Perl doesn't go looking for a variable named $VAR1 and complain when it can't find it.

Also, you have a subtle error in your code. The line if ($line = ~s/ \$VAR1/ my %hash /) is actually performing the search and replace on the default variable $_ (which hasn't been set to anything), taking the bitwise negation of the result (the number of replacements made, most likely zero) and assigning that result to the $line variable, which will most likely be true and execute the if block. The code should be like this: