in reply to loop/substitution problem
Please just note that your attempt to obfuscate your source code is mostly futile and will most likely just annoy anybody wishing to modify your software. If you want to prevent people from modifying it, make your contract such. The Perl debugger and/or the Deparse module will take care of your source code efficiently.
Having said that, the academic challenge of your question is still nice. Let me restate your problem : You want to filter out all names of all used variables (within some limits, you don't want to stomp on variables exported by other modules or Perl internal variables like $/), and change their names into something else. Note that this problem can be very hard to solve short of writing a complete Perl parser if you use modules.
As you are using strict (and if not, you should start using strict), all you have to look for will be lines containing the my codeword, take note of the variable name, come up with a better name for it, and then substitute the new name everywhere.
You already have a regular expression, which is not perfect but let's assume that we want to solve the first step, collecting all our own variable names, with a regular expression.
while ( /\G.*?my\s*([@$%]\w+(?:\s*,\s*[@$%]\w+)*\)/g ) { print $1,"\n"; };
This regular expression is nowhere near perfect, as it only matches variables declared like my $a,$b,@c, but not variables declared like my ($a,$b,@c) = @_. I leave the generalisation to you, it's not really hard, given what you have already. In fact, even then the method has its limits, as it will be hard to detect strings that look like variable names but are single quoted strings where no variable replacement will take place.
After you have collected the names of all variables used, you come up with new names and keep the correspondence in a hash.
Now, all there is left to do is to go over your source code once again and substitute every occurrence of any original variable name with the new variable name.
|
|---|