r25583 - in /branches/upstream/libproc-fork-perl/current: Build.PL Changes MANIFEST META.yml Makefile.PL eg/ eg/daemon.pl eg/multichild.pl eg/server.pl eg/simple.pl lib/Proc/Fork.pm lib/Proc/Fork/ lib/Proc/Fork/Runner.pm

ansgar-guest at users.alioth.debian.org ansgar-guest at users.alioth.debian.org
Wed Sep 24 16:41:39 UTC 2008


Author: ansgar-guest
Date: Wed Sep 24 16:41:35 2008
New Revision: 25583

URL: http://svn.debian.org/wsvn/pkg-perl/?sc=1&rev=25583
Log:
[svn-upgrade] Integrating new upstream version, libproc-fork-perl (0.71)

Added:
    branches/upstream/libproc-fork-perl/current/eg/
    branches/upstream/libproc-fork-perl/current/eg/daemon.pl
    branches/upstream/libproc-fork-perl/current/eg/multichild.pl
    branches/upstream/libproc-fork-perl/current/eg/server.pl
    branches/upstream/libproc-fork-perl/current/eg/simple.pl
    branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/
    branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/Runner.pm
Modified:
    branches/upstream/libproc-fork-perl/current/Build.PL
    branches/upstream/libproc-fork-perl/current/Changes
    branches/upstream/libproc-fork-perl/current/MANIFEST
    branches/upstream/libproc-fork-perl/current/META.yml
    branches/upstream/libproc-fork-perl/current/Makefile.PL
    branches/upstream/libproc-fork-perl/current/lib/Proc/Fork.pm

Modified: branches/upstream/libproc-fork-perl/current/Build.PL
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/Build.PL?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/Build.PL (original)
+++ branches/upstream/libproc-fork-perl/current/Build.PL Wed Sep 24 16:41:35 2008
@@ -6,7 +6,8 @@
 	module_name         => 'Proc::Fork',
 	license             => 'perl',
 	dist_author         => 'Aristotle Pagaltzis <pagaltzis at gmx.de>',
-	build_requires      => { 'Test::More' => 0 },
+	requires            => { 'Exporter::Tidy' => 0 },
+	build_requires      => { 'Test::More'     => 0 },
 	add_to_cleanup      => [ 'Proc-Fork-*' ],
 	create_makefile_pl  => 'traditional',
 );

Modified: branches/upstream/libproc-fork-perl/current/Changes
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/Changes?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/Changes (original)
+++ branches/upstream/libproc-fork-perl/current/Changes Wed Sep 24 16:41:35 2008
@@ -1,4 +1,34 @@
 =head1 Revision history for Proc-Fork
+
+
+=head2 0.71 (2008-09-22T03:09:02+0200)
+
+=over 4
+
+=item * Brown paper bag release.
+
+Forgot some mandatory parens. No idea why 5.8.8 didn't give me the syntax error that 5.10 detected.
+
+=back
+
+
+=head2 0.7 (2008-09-20 16:54:09+0200)
+
+=over 4
+
+=item * Completely new internal structure; modified API, now with a C<run_fork> wrapper.
+
+This should address both the semicolon problem and the context sensitivity problem pointed out on AnnoCPAN by dmcbride. Code written against the previous API design will continue to work unchanged.
+
+=item * L<Exporter::Tidy> replaces some local code.
+
+It's a very small pure-Perl module that shouldn't be a dependency burden, and it's a good design that deserves some advertisement.
+
+=item * POD examples extracted and provided as files in an F<eg/> directory.
+
+Yes, this is Kwalitee whoring. Hopefully I won't make changes to the docs that I then forget to update in the F<eg/> copies. Yay for CPANTS.
+
+=back
 
 
 =head2 0.61 (2007-12-23 12:06:22+0100)

Modified: branches/upstream/libproc-fork-perl/current/MANIFEST
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/MANIFEST?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/MANIFEST (original)
+++ branches/upstream/libproc-fork-perl/current/MANIFEST Wed Sep 24 16:41:35 2008
@@ -4,7 +4,12 @@
 t/00.mock.t
 t/01.real.t
 lib/Proc/Fork.pm
+lib/Proc/Fork/Runner.pm
 MANIFEST
 META.yml
 Makefile.PL
 t/cpants.t
+eg/daemon.pl
+eg/multichild.pl
+eg/server.pl
+eg/simple.pl

Modified: branches/upstream/libproc-fork-perl/current/META.yml
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/META.yml?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/META.yml (original)
+++ branches/upstream/libproc-fork-perl/current/META.yml Wed Sep 24 16:41:35 2008
@@ -1,18 +1,23 @@
 ---
 name: Proc-Fork
-version: 0.61
+version: 0.71
 author:
   - 'Aristotle Pagaltzis <pagaltzis at gmx.de>'
 abstract: 'Simple, intuitive interface to the fork() system call'
 license: perl
 resources:
   license: http://dev.perl.org/licenses/
+requires:
+  Exporter::Tidy: 0
 build_requires:
   Test::More: 0
 provides:
   Proc::Fork:
     file: lib/Proc/Fork.pm
-    version: 0.61
+    version: 0.71
+  Proc::Fork::Runner:
+    file: lib/Proc/Fork/Runner.pm
+    version: 0.71
 generated_by: Module::Build version 0.2808
 meta-spec:
   url: http://module-build.sourceforge.net/META-spec-v1.2.html

Modified: branches/upstream/libproc-fork-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/Makefile.PL?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/Makefile.PL (original)
+++ branches/upstream/libproc-fork-perl/current/Makefile.PL Wed Sep 24 16:41:35 2008
@@ -2,13 +2,14 @@
 use ExtUtils::MakeMaker;
 WriteMakefile
 (
-          'PL_FILES' => {},
-          'INSTALLDIRS' => 'site',
           'NAME' => 'Proc::Fork',
-          'EXE_FILES' => [],
           'VERSION_FROM' => 'lib/Proc/Fork.pm',
           'PREREQ_PM' => {
-                           'Test::More' => 0
-                         }
+                           'Exporter::Tidy' => '0',
+                           'Test::More' => '0'
+                         },
+          'INSTALLDIRS' => 'site',
+          'EXE_FILES' => [],
+          'PL_FILES' => {}
         )
 ;

Added: branches/upstream/libproc-fork-perl/current/eg/daemon.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/eg/daemon.pl?rev=25583&op=file
==============================================================================
--- branches/upstream/libproc-fork-perl/current/eg/daemon.pl (added)
+++ branches/upstream/libproc-fork-perl/current/eg/daemon.pl Wed Sep 24 16:41:35 2008
@@ -1,0 +1,13 @@
+use strict;
+use Proc::Fork;
+use POSIX;
+
+# One-stop shopping: fork, die on error, parent process exits.
+run_fork { parent { exit } };
+
+# Other daemon initialization activities.
+$SIG{INT} = $SIG{TERM} = $SIG{HUP} = $SIG{PIPE} = \&some_signal_handler;
+POSIX::setsid() or die "Cannot start a new session: $!\n";
+close $_ for *STDIN, *STDOUT, *STDERR;
+
+# rest of daemon program follows

Added: branches/upstream/libproc-fork-perl/current/eg/multichild.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/eg/multichild.pl?rev=25583&op=file
==============================================================================
--- branches/upstream/libproc-fork-perl/current/eg/multichild.pl (added)
+++ branches/upstream/libproc-fork-perl/current/eg/multichild.pl Wed Sep 24 16:41:35 2008
@@ -1,0 +1,36 @@
+use strict;
+use Proc::Fork;
+use IO::Pipe;
+
+my $num_children = 5;    # How many children we'll create
+my @children;            # Store connections to them
+$SIG{CHLD} = 'IGNORE';   # Don't worry about reaping zombies
+
+# Spawn off some children
+for my $num ( 1 .. $num_children ) {
+	# Create a pipe for parent-child communication
+	my $pipe = IO::Pipe->new;
+
+	# Child simply echoes data it receives, until EOF
+	run_fork { child {
+		$pipe->reader;
+		my $data;
+		while ( $data = <$pipe> ) {
+			chomp $data;
+			print STDERR "child $num: [$data]\n";
+		}
+		exit;
+	} };
+
+	# Parent here
+	$pipe->writer;
+	push @children, $pipe;
+}
+
+# Send some data to the kids
+for ( 1 .. 20 ) {
+	# pick a child at random
+	my $num = int rand $num_children;
+	my $child = $children[$num];
+	print $child "Hey there.\n";
+}

Added: branches/upstream/libproc-fork-perl/current/eg/server.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/eg/server.pl?rev=25583&op=file
==============================================================================
--- branches/upstream/libproc-fork-perl/current/eg/server.pl (added)
+++ branches/upstream/libproc-fork-perl/current/eg/server.pl Wed Sep 24 16:41:35 2008
@@ -1,0 +1,22 @@
+use strict;
+use IO::Socket::INET;
+use Proc::Fork;
+
+$SIG{CHLD} = 'IGNORE';
+
+my $server = IO::Socket::INET->new(
+	LocalPort => 7111,
+	Type      => SOCK_STREAM,
+	Reuse     => 1,
+	Listen    => 10,
+) or die "Couln't start server: $!\n";
+
+my $client;
+while ($client = $server->accept) {
+    run_fork { child {
+        # Service the socket
+        sleep(10);
+        print $client "Ooga! ", time % 1000, "\n";
+        exit; # child exits. Parent loops to accept another connection.
+    } }
+}

Added: branches/upstream/libproc-fork-perl/current/eg/simple.pl
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/eg/simple.pl?rev=25583&op=file
==============================================================================
--- branches/upstream/libproc-fork-perl/current/eg/simple.pl (added)
+++ branches/upstream/libproc-fork-perl/current/eg/simple.pl Wed Sep 24 16:41:35 2008
@@ -1,0 +1,30 @@
+use strict;
+use Proc::Fork;
+
+use IO::Pipe;
+my $p = IO::Pipe->new;
+
+run_fork {
+	parent {
+		my $child = shift;
+		$p->reader;
+		print while <$p>;
+		waitpid $child,0;
+	}
+	child {
+		$p->writer;
+		print $p "Line 1\n";
+		print $p "Line 2\n";
+		exit;
+	}
+	retry {
+		if( $_[0] < 5 ) {
+			sleep 1;
+			return 1;
+		}
+		return 0;
+	}
+	error {
+		die "That's all folks\n";
+	}
+};

Modified: branches/upstream/libproc-fork-perl/current/lib/Proc/Fork.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/lib/Proc/Fork.pm?rev=25583&op=diff
==============================================================================
--- branches/upstream/libproc-fork-perl/current/lib/Proc/Fork.pm (original)
+++ branches/upstream/libproc-fork-perl/current/lib/Proc/Fork.pm Wed Sep 24 16:41:35 2008
@@ -2,74 +2,57 @@
 
 package Proc::Fork;
 
-$VERSION = 0.61; # also change it in the docs
+$VERSION = 0.71; # also change it in the docs and in Fork/Runner.pm
 
 use strict;
 use warnings;
+use vars '$RUN_CLASS';
+
+$RUN_CLASS ||= 'Proc::Fork::Runner';
+eval "require $RUN_CLASS" or die $@;
+
+sub run_fork(&) {
+	my ( $setup ) = @_;
+	my $runner = $setup->();
+	if ( not eval { $runner->isa( $RUN_CLASS ) } ) {
+		require Carp;
+		Carp::croak( "Syntax error (trailing garbage in block after Proc::Fork setup?)" );
+	}
+	$runner->run;
+	return;
+}
 
 my $make_forkblock = sub {
 	my ( $config_key ) = shift;
-
-	return sub (&;$) {
+	sub (&;$) {
 		my ( $val, $config ) = @_;
 
 		# too many arguments or not a config hash as 2nd argument?
 		# then the user has almost certainly forgotten the trailing semicolon
-		if ( @_ > 2 or ( @_ == 2 and not eval { $config->isa( __PACKAGE__ ) } ) ) {
+		if ( @_ > 2 or ( @_ == 2 and not eval { $config->isa( $RUN_CLASS ) } ) ) {
 			require Carp;
 			Carp::croak( "Syntax error (missing semicolon after $config_key clause?)" );
 		}
 
-		$config ||= bless {}, __PACKAGE__;
-		$config->{ $config_key } = $val;
+		( $config ||= $RUN_CLASS->new )->set( $config_key, $val );
 
 		# if not called in void context, then we're not the final part of the call
 		# chain, so just pass the config up the chain
-		return $config if defined wantarray;
-
-		# otherwise, we pull the trigger
-		my ( $p, $c, $e, $r ) = delete @{ $config }{ qw( parent child error retry ) };
-
-		my $pid;
-
-		{
-			my $retry;
-
-			do {
-				$pid = fork;
-			} while ( not defined $pid ) and ( $r and $r->( ++$retry ) );
-		}
-
-		if    ( not defined $pid ) { $e ? $e->()       : die "Cannot fork: $!\n" }
-		elsif ( $pid )             { $p ? $p->( $pid ) : 0 }
-		else                       { $c ? $c->()       : 0 }
-
-		return;
+		defined wantarray ? return $config : $config->run;
 	};
 };
 
-my %block = map { $_ => $make_forkblock->( $_ ) } qw( child error retry parent );
-
-sub import {
-	my $class = shift;
-	my $pkg = caller;
-
-	@_ = keys %block if not @_;
-
-	for my $name ( @_ ) {
-		if ( not exists $block{ $name } ) {
-			require Carp;
-			Carp::croak( qq{"$name" is not exported by the $class module} );
-		}
-
-		no strict;
-		*{ $pkg . '::' . $name } = $block{ $name };
-	}
-
-	return 1;
-}
-
-__PACKAGE__->import;
+require Exporter::Tidy;
+Exporter::Tidy->import(
+	default => [ ':all' ],
+	wrapper => [ 'run_fork' ],
+	blocks  => [ $RUN_CLASS->blocks ],
+	_map    => { map { $_ => $make_forkblock->( $_ ) } $RUN_CLASS->blocks },
+);
+
+__PACKAGE__->import( ':blocks' );
+
+1;
 
 __END__
 
@@ -79,46 +62,45 @@
 
 =head1 VERSION
 
-This documentation describes Proc::Fork version 0.61
+This documentation describes Proc::Fork version 0.71
 
 =head1 SYNOPSIS
 
  use Proc::Fork;
 
- child {
-     # child code goes here.
- }
- parent {
-     my $child_pid = shift;
-     # parent code goes here.
-     waitpid $child_pid, 0;
- }
- retry {
-     my $attempts = shift;
-     # what to do if if fork() fails:
-     # return true to try again, false to abort
-     return if $attempts > 5;
-     sleep 1, return 1;
- }
- error {
-     # Error-handling code goes here
-     # (fork() failed and the retry block returned false)
+ run_fork {
+     child {
+         # child code goes here.
+     }
+     parent {
+         my $child_pid = shift;
+         # parent code goes here.
+         waitpid $child_pid, 0;
+     }
+     retry {
+         my $attempts = shift;
+         # what to do if if fork() fails:
+         # return true to try again, false to abort
+         return if $attempts > 5;
+         sleep 1, return 1;
+     }
+     error {
+         # Error-handling code goes here
+         # (fork() failed and the retry block returned false)
+     }
  };
- # Note the semicolon at the end! Necessary in most cases
 
 =head1 DESCRIPTION
 
 This module provides an intuitive, Perl-ish way to write forking programs by letting you use blocks to illustrate which code section executes in which fork. The code for the parent, child, retry handler and error handler are grouped together in a "fork block". The clauses may appear in any order, but they must be consecutive (without any other statements in between).
 
-The semicolon after the last clause is B<mandatory>, unless the last clause is at the end of the enclosing block or file.
-
 All four clauses need not be specified. If the retry clause is omitted, only one fork will be attempted. If the error clause is omitted the program will die with a simple message if it can't retry. If the parent or child clause is omitted, the respective (parent or child) process will start execution after the final clause. So if one or the other only has to do some simple action, you need only specify that one. For example:
 
  # spawn off a child process to do some simple processing
- child {
+ run_fork { child {
      exec '/bin/ls', '-l';
      die "Couldn't exec ls: $!\n";
- };
+ } };
  # Parent will continue execution from here
  # ...
 
@@ -126,23 +108,29 @@
 
 =head1 INTERFACE
 
+=head2 run_fork
+
+ run_fork { ... }
+
+Performs the fork operation configured in its block.
+
 =head2 child
 
  child { ... }
 
-This function executes the code reference passed to it if it discovers that it is the child process.
+Declares the block that should run in the child process.
 
 =head2 parent
 
  parent { ... }
 
-This function executes the code reference passed to it if it discovers that it is the parent process. It passes the child's PID to the code.
+Declares the block that should run in the parent process. The child's PID is passed as an argument to the block.
 
 =head2 retry
 
  retry { ... }
 
-This function executes the code reference passed to it if there was an error, ie if C<fork> returned undef. If the code returns true, another C<fork> is attempted. The function passes the number of fork attempts so far to the code.
+Declares the block that should run in case of an error, ie. if C<fork> returned C<undef>. If the code returns true, another C<fork> is attempted. The number of fork attempts so far is passed as an argument to the block.
 
 This can be used to implement a wait-and-retry logic that may be essential for some applications like daemons.
 
@@ -152,50 +140,44 @@
 
  error { ... }
 
-This function executes the code reference passed to it if there was an error, ie C<fork> returned undef and the C<retry> clause returned false. The function passes the number of forks attempted to the code.
+Declares the block that should run if there was an error, ie when C<fork> returns C<undef> and the C<retry> clause returns false. The number of forks attempted is passed as an argument to the block.
 
 If an C<error> clause is not used, errors will raise an exception using C<die>.
 
-=head1 SYNTAX NOTE
-
-B<Imporant note:> Due to the way Perl 5 parses these functions, there must be a semicolon after the close brace of the final clause, whether it be a C<parent>, C<child>, C<retry> or C<error> clause, unless that closing brace is the final token of the enclosing block or file.
-
-Proc::Fork attempts to detect missing semicolons.  How well this works remains to be seen.
-
 =head1 EXAMPLES
 
-=head2 Simple example
-
- # example with IPC via pipe
+=head2 Simple example with IPC via pipe
+
  use strict;
+ use Proc::Fork;
+
  use IO::Pipe;
- use Proc::Fork;
- my $p = new IO::Pipe;
-
- parent {
-     my $child = shift;
-     $p->reader;
-     print while ( <$p> );
-     waitpid $child,0;
- }
- child {
-     $p->writer;
-     print $p "Line 1\n";
-     print $p "Line 2\n";
-     exit;
- }
- retry {
-     if( $_[0] < 5 ) {
-		 sleep 1;
-		 return 1;
-     }
-     return 0;
- }
- error {
-     die "That's all folks\n";
+ my $p = IO::Pipe->new;
+
+ run_fork {
+     parent {
+         my $child = shift;
+         $p->reader;
+         print while <$p>;
+         waitpid $child,0;
+     }
+     child {
+         $p->writer;
+         print $p "Line 1\n";
+         print $p "Line 2\n";
+         exit;
+     }
+     retry {
+         if( $_[0] < 5 ) {
+             sleep 1;
+             return 1;
+         }
+         return 0;
+     }
+     error {
+         die "That's all folks\n";
+     }
  };
-
-(The terminating semicolon is not strictly necessary here, because the program ends there anyway, but it is good habit.)
 
 =head2 Multi-child example
 
@@ -210,10 +192,10 @@
  # Spawn off some children
  for my $num ( 1 .. $num_children ) {
      # Create a pipe for parent-child communication
-     my $pipe = new IO::Pipe;
+     my $pipe = IO::Pipe->new;
 
      # Child simply echoes data it receives, until EOF
-     child {
+     run_fork { child {
          $pipe->reader;
          my $data;
          while ( $data = <$pipe> ) {
@@ -221,7 +203,7 @@
              print STDERR "child $num: [$data]\n";
          }
          exit;
-     };
+     } };
 
      # Parent here
      $pipe->writer;
@@ -238,24 +220,22 @@
 
 =head2 Daemon example
 
- # daemon example
  use strict;
- use Proc::Fork ();
- use Posix;
+ use Proc::Fork;
+ use POSIX;
 
  # One-stop shopping: fork, die on error, parent process exits.
- Proc::Fork::parent {exit};
+ run_fork { parent { exit } };
 
  # Other daemon initialization activities.
  $SIG{INT} = $SIG{TERM} = $SIG{HUP} = $SIG{PIPE} = \&some_signal_handler;
- Posix::setsid() or die "Cannot start a new session: $!\n";
+ POSIX::setsid() or die "Cannot start a new session: $!\n";
  close $_ for *STDIN, *STDOUT, *STDERR;
 
  # rest of daemon program follows
 
-=head2 Forking network server example
-
- # Socket-based server example
+=head2 Forking socket-based network server example
+
  use strict;
  use IO::Socket::INET;
  use Proc::Fork;
@@ -263,20 +243,20 @@
  $SIG{CHLD} = 'IGNORE';
 
  my $server = IO::Socket::INET->new(
-	LocalPort => 7111,
-	Type      => SOCK_STREAM,
-	Reuse     => 1,
-	Listen    => 10,
+     LocalPort => 7111,
+     Type      => SOCK_STREAM,
+     Reuse     => 1,
+     Listen    => 10,
  ) or die "Couln't start server: $!\n";
 
  my $client;
  while ($client = $server->accept) {
-     child {
+     run_fork { child {
          # Service the socket
          sleep(10);
          print $client "Ooga! ", time % 1000, "\n";
          exit; # child exits. Parent loops to accept another connection.
-     }
+     } }
  }
 
 =head1 EXPORTS
@@ -285,6 +265,8 @@
 
 =over 4
 
+=item * C<run_fork>
+
 =item * C<child>
 
 =item * C<parent>
@@ -297,7 +279,7 @@
 
 =head1 DEPENDENCIES
 
-L<Carp> and L<Exporter>, which are part of the Perl distribution.
+L<Carp>, which is part of the Perl distribution, and L<Exporter::Tidy>.
 
 =head1 BUGS AND LIMITATIONS
 
@@ -309,11 +291,11 @@
 
 Aristotle Pagaltzis, L<mailto:pagaltzis at gmx.de>
 
-Original version and most of the documentation by Eric J. Roode.
+Documentation by Eric J. Roode.
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright (c) 2005 by Aristotle Pagaltzis. All rights Reserved.
+Copyright (c) 2005-2008 by Aristotle Pagaltzis. All rights Reserved.
 
 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.
 

Added: branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/Runner.pm
URL: http://svn.debian.org/wsvn/pkg-perl/branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/Runner.pm?rev=25583&op=file
==============================================================================
--- branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/Runner.pm (added)
+++ branches/upstream/libproc-fork-perl/current/lib/Proc/Fork/Runner.pm Wed Sep 24 16:41:35 2008
@@ -1,0 +1,58 @@
+#!/usr/bin/perl
+
+package Proc::Fork::Runner;
+
+$VERSION = 0.71;
+
+use strict;
+use warnings;
+
+sub new {
+	my $self = bless {}, shift;
+	@{ $self }{ $self->blocks } = ();
+	return $self;
+}
+
+sub blocks { qw( parent child error retry ) }
+
+sub set {
+	my $self = shift;
+	my ( $name, $callback ) = @_;
+
+	if ( not exists $self->{ $name } ) {
+		require Carp;
+		Carp::croak( "Attempt to set invalid ${\ref $self} attribute '$name'" );
+	}
+
+	if ( 'CODE' ne ref $callback ) {
+		require Carp;
+		Carp::croak( "Attempt to set ${\ref $self} attribute to value that is not a CODE reference" );
+	}
+
+	$self->{ $name } = $callback;
+	return $self;
+}
+
+sub run {
+	my $self = shift;
+
+	my ( $p, $c, $e, $r ) = @{ $self }{ $self->blocks };
+
+	my $pid;
+
+	{
+		my $retry;
+
+		do {
+			$pid = fork;
+		} while ( not defined $pid ) and ( $r and $r->( ++$retry ) );
+	}
+
+	if    ( not defined $pid ) { $e ? $e->()       : die "Cannot fork: $!\n" }
+	elsif ( $pid )             { $p ? $p->( $pid ) : 0 }
+	else                       { $c ? $c->()       : 0 }
+
+	return;
+}
+
+1;




More information about the Pkg-perl-cvs-commits mailing list