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.


In reply to Re: loop/substitution problem by Corion
in thread loop/substitution problem by cghost23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.