Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: How to convert bunch of perl file + other dependent Input files Into a single executable by marioroy
in thread How to convert bunch of perl file + other dependent Input files Into a single executable by rockyurock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-28 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found