Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Perl compiling

XfactorXfactor Posts: 343Member
After searching past posts on here, I see perl2exe was suggested as a means to compile perl scripts.
I guess I'm just wondering if this is still the suggested means. I also found a script on my box called perlcc, but I've only gotten it to work with the -B option which compiles the script into bytecode.
I just would like to know other peoples experiences and/or preferences.

Any and all suggestions are appreciated.

X

Comments

  • JonathanJonathan Posts: 2,914Member
    : After searching past posts on here, I see perl2exe was suggested as a
    : means to compile perl scripts.
    Apart from I believe it doesn't actually compile scripts, or not in the sense I think you're using compile (e.g. generating a native executable version of your code). It simply bundles your code and the core of the Perl interpreter into an executable along with a loader that extracts your code from the executable (though often into memory rather than to disk, to help protect the code) and runs the code with the embedded Perl interpreter.

    : I guess I'm just wondering if this is still the suggested means.
    This depends on what your motivation for "compiling" is, really.

    : I also found a script on my box called perlcc, but I've only gotten it
    : to work with the -B option which compiles the script into bytecode.
    All of that stuff is rather experimental. The perlcc docs themselves say:-

    "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."

    For the best chance of success, use the latest Perl you can lay your hands on.

    The docs can be found here:-
    http://www.perldoc.com/perl5.8.4/bin/perlcc.html

    I'll make a few notes that I don't think you'll find it the docs. Firstly, for anything other than the -B option to work out you're going to need to have a C compiler handy. The errors may be your C compiler choking on the code that the C backends churn out, but may also be because you're missing one. Feel free to post any errors here and I might be able to help you out on this.

    Secondly, compiling to bytecode for performance reasons is a waste of time. It often means that the startup time of your program is greater, not less as you might expect. To attempt to preempt your "why", the Perl 5 interpreter is a tree-walking interpreter, not a bytecode interpreter (making it different from the .NET runtime or the JVM), and it seems to work out that it's as quick to construct the tree from the source as it is to re-construct it from the bytecode. I should probably add that I haven't personally benchmarked this, but the words of a Perl 5 internals developer on this are good enough for me. :-)

    : I just would like to know other peoples experiences and/or
    : preferences.
    :
    : Any and all suggestions are appreciated.
    :
    Personally, I just don't. The way to go really depends on what your aims are and what problems you think compilation will solve for you, though.

    That's the present situation, but I do have some good news for you. The Ponie project is currently re-implementing Perl 5 on top of Parrot, the runtime engine being developed to run Perl 6 as well as a whole range of other languages. Parrot *is* a bytecode interpreter, meaning we can get a time saving by compling to bytecode and keeping our program in that. Becuase for many platforms Parrot has JIT support, it wasn't a massive step to take this and use it to generate native executable code, and as such Parrot can now produce native executables. At first this only worked for some UNIX-y OSes, but not so long ago somebody contributed a patch to generate Win32 executables too. I haven't had chance to test it yet, but it looks every promising and almost certainly will be good enough for production use.

    The bad news is that Ponie currently won't compile on Windows. I'm some way into solving that one, though. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • XfactorXfactor Posts: 343Member
    I'm wanting to create a perl script which is unreadable by a user by creating a binary file or whatever. I know any method isnt' going to keep everyone from "uncompiling" and reading the source but it'll keep the not so techy people from reading it.

    Thanks for your reply so far.

    X
  • JonathanJonathan Posts: 2,914Member
    : I'm wanting to create a perl script which is unreadable by a user by
    : creating a binary file or whatever. I know any method isnt' going to
    : keep everyone from "uncompiling" and reading the source but it'll keep
    : the not so techy people from reading it.
    :
    In that case, perl2exe is probably the easiest option if you're prepared to pay for it (or, IIRC, there may be a free version that meets your needs?) The Bytecode back end doesn't seem to work on Win32, though I have got it to generate some code on Linux. I suggest you do your own experiments. However, be aware that you can use the Deparse module to easily turn byteocde back into readable Perl source. Here's an example where I generate bytecode, then later de-parse it back to readable Perl.

    [code]kern:~$ cat test2.pl
    #!/usr/bin/perl

    print "Hello world!
    ";

    kern:~$ perlcc -o test2b.pl -B test2.pl
    kern:~$ perl test2b.pl
    Hello world!
    kern:~$ cat test2b.pl
    #!/usr/bin/perl
    use ByteLoader 0.04;
    PLBCi386-linux0.040x1234

    C
    :9877 STWZ@[VY
    STWX.,VYZ

    main
    test2.pl
    S
    TWX/,VYZm oFn
    pqr
    STWX2,VYZ[


    S
    T
    WX0,VYZ
    STWX1,VYZe
    Hello world!
    `RPLributes::bootstrap
    H
    FIJKNM

    xsutils.c
    0.0C/.-034251tsu
    kern:~$ perl -MO=Deparse test2b.pl > test2b-decomp.pl
    test2b.pl syntax OK
    kern:~$ cat test2b-decomp.pl
    print "Hello world!
    ";
    kern:~$
    [/code]

    I'm not sure quite how well the DeParse module is known. It's most certainly a cool tool for figuring out obsfucated code...especially if you stick in the -p option to stick in extra brackets to clarify precedence. :-) Docs here in case you're interested...
    http://www.perl.com/doc/manual/html/lib/B/Deparse.html

    I guess how well the bytecode approach would work depends on your definition of "not so techy". It wouldn't fox me for long, but I suspect I fall well inside the geek camp...

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.