Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Perl script compressor

by haukex (Archbishop)
on Dec 08, 2019 at 08:14 UTC ( [id://11109834]=note: print w/replies, xml ) Need Help??


in reply to Perl script compressor

I am not even sure what kind of complicated code patterns may exist in a perl script.

Classic example of "only perl can parse Perl" (based heavily on tye's example here):

BEGIN { eval( rand>0.5 ? 'sub foo () {}' : 'sub foo {}' ) } foo / # /; 42;

That's perfectly valid Perl code even under strict. Run that though B::Deparse a few times, and sometimes you'll get

foo(/ # /); '???';

and sometimes

&foo() / 42;

In other words, sometimes it's a function call to which the result of a regex is being passed followed by a constant (that gets optimized away), and sometimes it's the return value of a function call, followed by a division operator, a comment, and the divisor. Which it is depends entirely on a random number that changes from run to run, and the result can't be known until the perl binary executes the first line of Perl code; doing the same with a static parse (without executing code) is impossible.

I need to come up with a logic that can interpret a line of complex perl code and remove comments and spaces in such a way that it won't break the code.

Perl Cannot Be Parsed: A Formal Proof

The closest thing to a static Perl parser is PPI (and perhaps PPR; Update 2: and new: Guacamole), and marto already pointed you to one module based on it; see also Perl::Squish. Update: You could also look at Perl::Tidy. Update 3: I think RPerl also implements its own parser.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found