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.