Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Compressing/Obfuscating a Javascript file

by tachyon (Chancellor)
on Oct 06, 2001 at 07:54 UTC ( #117165=note: print w/replies, xml ) Need Help??


in reply to Compressing/Obfuscating a Javascript file

To do this properly is difficult. As a start you need to do somthing like this:

local $/; # undef $/ so we glob data in $data = <DATA>; # get all our data as a string $data =~ s|//.*\n|\n|g; # strip single line comments $data =~ s|/\*.*?\*/||gs; # strip multi-line comments $data =~ s|^\s*\n||gm; # strip blank lines $data =~ s|^\s+||gm; # strip leading whitespace $data =~ s|\s+$||gm; # strip trailing whitespace # do the magic and split into functions @functions = split /(?=\bfunction\s)/, $data; # pudding proof print "$_ ---\n" for @functions; __DATA__ var answer = 42; // just because function 1 { blah // comment; } /* this function is no good function 2 { blah } */ function 3 { if (functionvar){ // var name not function keyword // do the blah stuff } } /* this is another comment */ function 4 { just another perl hacker }

You need to remove all the comments first to ensure that the only occurences of the word function represent keywords. You can then just use split with a positive lookahead assertion in the regex to generate your array. The next step you want to do is much harder but that is another story.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117165]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-03-28 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (167 votes). Check out past polls.

    Notices?