| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | AnyEvent::Fork::Early - avoid having to exec another perl interpreter | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | # only usable in the main program, and must be called | ||||||||||
| 8 | # as early as possible | ||||||||||
| 9 | |||||||||||
| 10 | #!/usr/bin/perl | ||||||||||
| 11 | use AnyEvent::Fork::Early; | ||||||||||
| 12 | |||||||||||
| 13 | # now you can do other stuff | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | L<AnyEvent::Fork> normally spawns a new perl process by executing the perl | ||||||||||
| 18 | binary. It does this because it is the only way to get a "clean state", as | ||||||||||
| 19 | the program using it might have loaded modules that are not fork friendly | ||||||||||
| 20 | (event loops, X11 interfaces and so on). | ||||||||||
| 21 | |||||||||||
| 22 | However, in some cases, there is no external perl interpreter to execute, | ||||||||||
| 23 | for example, when you use L<App::Staticperl> or L<PAR::Packer> to embed | ||||||||||
| 24 | perl into another program, and that program runs on another system without | ||||||||||
| 25 | perl installed. | ||||||||||
| 26 | |||||||||||
| 27 | And anyway, forking would still be more efficient, if it were possible. | ||||||||||
| 28 | |||||||||||
| 29 | And, as you hopefully guessed, this module makes this possible - it must | ||||||||||
| 30 | be run by the main program (i.e. to cannot be used in a module), and as | ||||||||||
| 31 | early as possible. How early? Well, early enough so that any other modules | ||||||||||
| 32 | can still be loaded and used, that is, before modules such as AnyEvent or | ||||||||||
| 33 | Gtk2 are being initialised. | ||||||||||
| 34 | |||||||||||
| 35 | Upon C<use>'ing the module, the process is forked, and the resulting | ||||||||||
| 36 | process is used as a template process for C<new> and C<new_exec>, so | ||||||||||
| 37 | everything should just work out. | ||||||||||
| 38 | |||||||||||
| 39 | Please resist the temptation to delay C<use>ing this module to | ||||||||||
| 40 | preload more modules that could be useful for your own purposes, see | ||||||||||
| 41 | L<AnyEvent::Fork::Template> for that. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | package AnyEvent::Fork::Early; | ||||||||||
| 46 | |||||||||||
| 47 | # load stuff we need anyways | ||||||||||
| 48 | 3 | ▶ | use AnyEvent::Fork (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | # this does not work on win32, due to the atrociously bad fake perl fork | ||||||||||
| 51 | 1 | ▶ | unless ($^O eq "MSWin32") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | # we preload certain modules because sooner or later, somebody will use them. | ||||||||||
| 53 | # complain to me if that causes trouble. | ||||||||||
| 54 | 1 | require common::sense; | |||||||||
| 55 | 1 | require strict; | |||||||||
| 56 | 1 | require warnings; | |||||||||
| 57 | 1 | ▶ | require feature if $] >= 5.010; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | require Carp; | |||||||||
| 59 | |||||||||||
| 60 | 1 | require IO::FDPass; | |||||||||
| 61 | |||||||||||
| 62 | 1 | $AnyEvent::Fork::TEMPLATE = | |||||||||
| 63 | $AnyEvent::Fork::EARLY = AnyEvent::Fork->_new_fork ("AnyEvent::Fork::Early"); | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | =head1 AUTHOR | ||||||||||
| 67 | |||||||||||
| 68 | Marc Lehmann <schmorp@schmorp.de> | ||||||||||
| 69 | http://home.schmorp.de/ | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 1 | ||||||||||
| 74 | 1 | ||||||||||