Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does PAR::Packer work?

I was using PAR::Packer and this question popped up in my mind. How does PAR::Packer work in Perl? Does it actually compile the Perl script to .exe like g++ compiles C++ Sources to .exe or does it work like py2exe in Python that packs the interpreter and the script into an .exe?

like image 406
sirius_x Avatar asked Aug 14 '13 17:08

sirius_x


2 Answers

To make this absolutely clear:

  • Tools like PAR::Packer do not “compile” your Perl script. They bundle the perl interpreter together with your source files and any required modules into a big fat executable file. When it is run, the original sources are extracted and fed to the enclosed perl.

    This works reasonably well, but does not yield a speed improvement (on the contrary…). The only advantage is that you can distribute your programs as a single (albeit quite large) file, without dependencies.

  • There is a very experimental tool called perlcc that is able to translate some Perl programs to C or a Perl bytecode serialization. As the docs put it:

    The code generated in this way is not guaranteed to work. The whole codegen suite (perlcc included) should be considered very experimental. Use for production purposes is strongly discouraged.

    This is because the Perl language does not support static compilation. It needs to be able to execute code during parsing for some dynamic features during the same session where the main execution phase takes place.

There are other, commercial tools, that usually fall in the same category as PAR::Packer (creating fat executables).

Summary: If you want a single executable, use PAR::Packer. If you want speed, inline some C (or use XS). There is no tool that can compile all Perl scripts to machine code.

like image 170
amon Avatar answered Nov 07 '22 10:11

amon


I was using PAR::Packer and this question popped up in my mind. How does PAR::Packer work in Perl?

Does it actually compile the Perl script to .exe like g++ compiles C++ Sources to .exe

no pp and perl2exe doesn't. (though pp is free).

but it looks like perlcc does

or does it work like py2exe in Python that packs the interpreter and the script into an .exe?

pp and perl2exe, yes

As an example- sendemail.exe that looks like something done in PAR Packer or Perl2exe It packs the interpreter into the exe.

You can open sendemail.exe in 7-zip! There are some folders there but one can't really see it actual files.

I suppose it's some form of self extracting executable, but, that executes code.

You can monitor it with process monitor, and you see.

Or with, process explorer and see if it uses any dll from the temp directory that it creates..

It creates a temp directory e.g.

C:\Users\user\AppData\Local\Temp\pdk-user <--- Win7

or

C:\Documents and Settings\user\Local Settings\Temp\pdk-user <-- WinXP

The temporary directory it creates contains a bunch of strange named DLLs and a DLL called perl58.dll which is no doubt, the perl interpreter

I'm sure at one point I saw it had a dll there with a normal name. SSLEAY32.DLL and interestingly when I ran the pl file it used a dll with a similar name from my c:\perl64 directory. So the EXE looks like a bit of a hack really. It's more reliable to run the pl file directly.

DIR /s/b within that directory, shows-

178c2b604baa8a7f1ebc80539f378bff.dll
1823e8f62785746fd29cf0b06c636600.dll
465d2954d90fe6225ea61b3907c91da8.dll
6145f78a34d5ced8200800f1455d578a <-- the directory with the perl58. dll
9c50b5816b0e35f047e41f5899721d46.dll
f4e2e0db77ed1e6572c2f490479cd815.dll
f72f556d99dfb6b0c3bb37f123e2ee96.dll
6145f78a34d5ced8200800f1455d578a\perl58.dll

no normal named DLLs showing other than perl58.dll (though I have seen a normal one there in the past).

if you look in process explorer, you see it using perl58.dll and other normal named dlls

If you look at the PAR website, a page describes perl2exe

Perl2Exe is commercial, command-line application that can build standalone executables from perl sources. It works by creating an executable that contains

A standalone perl interpreter (that is capable of grokking perl 5.8.x) Your perl script and All perl modules that are referenced by your perl script.

pp - PAR Packager provides that same functionality, but is free.

And if you look here

http://www.perlmonks.org/?node_id=237943

...the perl2exe tool is not a way to hide your source code.
Now it's even come to the attention of the security community (reported in bugtraq, for example).

For details, see the report from net-security's page.

Please stop supporting perl2exe. Please use PAR for a complete installation package, or perlcc to simply compile the top-level program.

-- Randal L. Schwartz, Perl hacker 

So, PP and perl2exe combine the interpreter into the EXE, And you can get an idea that there's something funny atypical about the EXE when it opens in 7zip!

perlcc compiles properly.

it appears to have been unmaintained for a while http://www.perlmonks.org/?node_id=654568 , people said it was buggy, but some work was done on it as recently as June 2014 http://www.yapcna.org/yn2014/talk/5603 It gets better performance than interpreted perl. (unlike the EXEs that pack an interpreter, which are slower than running a perl script normally).

like image 30
barlop Avatar answered Nov 07 '22 10:11

barlop