http://qs1969.pair.com?node_id=1183510


in reply to How to convert bunch of perl file + other dependent Input files Into a single executable

Updated on August 23, 2017.

Greetings, rockyurock.

The following are cross-platform templates I share with folks, requesting how to compile a Perl script to an executable for MCE and MCE::Hobo. Making an executable is possible with the PAR::Packer module. On the Windows platform, threads, threads::shared, and exiting via threads are necessary for the binary to exit successfully.

In regards to module dependencies, one way is to include them at the top of the script.

# https://metacpan.org/pod/PAR::Packer # https://metacpan.org/pod/pp # # pp -o demo.exe demo.pl # ./demo.exe use strict; use warnings; use if $^O eq "MSWin32", "threads"; use if $^O eq "MSWin32", "threads::shared"; # Include minimum dependencies for MCE. # Add other modules required by your application here. use Storable (); use Time::HiRes (); # use Sereal (); # optional, for faster serialization use MCE; my $mce = MCE->new( max_workers => 4, user_func => sub { print "hello there from ", MCE->wid(), "\n"; } ); $mce->run(); threads->exit(0) if $INC{"threads.pm"};

MCE workers above spawn as threads whenever threads is present. Unlike MCE, MCE::Hobo workers can only run as child processes, spawned via fork. To work around PAR or a dependency not being multi-process safe, one must run inside a thread on the Windows platform or the exe will crash.

# https://metacpan.org/pod/PAR::Packer # https://metacpan.org/pod/pp # # pp -o demo.exe demo.pl # ./demo.exe use strict; use warnings; use if $^O eq "MSWin32", "threads"; use if $^O eq "MSWin32", "threads::shared"; # Include minimum dependencies for MCE::Hobo. # Add other modules required by your application here. use Storable (); use Time::HiRes (); # use IO::FDPass (); # optional: for condvar, handle, queue # use Sereal (); # optional: for faster serialization use MCE::Hobo; use MCE::Shared; # For PAR to work on the Windows platform, one must include manually # any shared modules used by the application. # use MCE::Shared::Array; # for MCE::Shared->array # use MCE::Shared::Cache; # for MCE::Shared->cache # use MCE::Shared::Condvar; # for MCE::Shared->condvar # use MCE::Shared::Handle; # for MCE::Shared->handle, mce_open # use MCE::Shared::Hash; # for MCE::Shared->hash # use MCE::Shared::Minidb; # for MCE::Shared->minidb # use MCE::Shared::Ordhash; # for MCE::Shared->ordhash # use MCE::Shared::Queue; # for MCE::Shared->queue # use MCE::Shared::Scalar; # for MCE::Shared->scalar # Et cetera. Only load modules needed for your application. use MCE::Shared::Sequence; # for MCE::Shared->sequence my $seq = MCE::Shared->sequence( 1, 9 ); sub task { my ( $id ) = @_; while ( defined ( my $num = $seq->next() ) ) { print "$id: $num\n"; sleep 1; } } sub main { MCE::Hobo->new( \&task, $_ ) for 1 .. 3; MCE::Hobo->waitall(); } # Main must run inside a thread on the Windows platform or workers # will fail duing exiting, causing the exe to crash. The reason is # that PAR or a dependency isn't multi-process safe. ( $^O eq "MSWin32" ) ? threads->create(\&main)->join() : main(); threads->exit(0) if $INC{"threads.pm"};

Kind regards, Mario.

  • Comment on Re: How to convert bunch of perl file + other dependent Input files Into a single executable
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to convert bunch of perl file + other dependent Input files Into a single executable
by rockyurock (Acolyte) on Mar 03, 2017 at 08:03 UTC
    Okk Thank you! But Still I did not get how It can help me to make the .exe on windows. Do I need to Include It my program as when I execute It stand alone It's givnig me below Error :
    Can't locate IO/FDPass.pm in @INC (you may need to install the IO::FDP +ass module ) (@INC contains: C:/Perl64/site/lib C:/Perl64/lib .) at E:\Automation + Related\P erl Scripts - ALL\IMS_debugging\Perl_to_Exe\P2E.pl line 16. BEGIN failed--compilation aborted at E:\Automation Related\Perl Script +s - ALL\IM S_debugging\Perl_to_Exe\P2E.pl line 16.
    Also I think PAR::Packer is already Installed on my system as when I tried -> ppm install PAR::Packer it showed Downloading ActiveState Package Repository dbimage...not modified No missing packages to install
        Hi.. It;s giving me the below Error :

        Can't locate Proc/Background.pm in @INC (you may need to install the Proc::Backg round module) (@INC contains: C:/Perl64/site/lib C:/Perl64/lib .) at E:\Automati on Related\Perl Scripts - ALL\IMS_debugging\Perl_to_Exe\ppsimple.pl line 38. BEGIN failed--compilation aborted at E:\Automation Related\Perl Scripts - ALL\IM S_debugging\Perl_to_Exe\ppsimple.pl line 38.

      The modules or dependencies must be available in Perl. Remove the dependencies not needed, including MCE if not used. Basically, add only dependencies required by your application.