1 package IPC::MorseSignals;
6 use Time::HiRes qw/usleep/;
7 use POSIX qw/SIGUSR1 SIGUSR2/;
11 IPC::MorseSignals - Communicate between processes with Morse signals.
19 our $VERSION = '0.01';
23 use IPC::MorseSignals qw/msend mrecv/;
27 die "fork() failed: $!";
29 local @SIG{qw/USR1 USR2/} = mrecv sub { print STDERR "recieved $_[0]!\n" };
32 msend "hello!\n" => $pid;
37 This module implements a rare form of IPC by sending Morse-like signals through C<SIGUSR1> and C<SIGUSR2>. It uses both signals C<SIGUSR1> and C<SIGUSR2>, so you won't be able to keep them for something else when you use this module.
39 But, seriously, use something else for your IPC. :)
45 msend $msg, $pid [, $speed ]
47 Sends the string C<$msg> to the process C<$pid> (or to all the processes C<@$pid> if $pid is an array ref) at C<$speed> bits per second. Default speed is 1000, don't set it too low or the target will miss bits and the whole message will be crippled.
52 my ($msg, $pid, $speed) = @_;
53 my @pid = (ref $pid eq 'ARRAY') ? @$pid : $pid;
54 return unless @pid && $msg;
56 my $delay = int(1_000_000 / $speed);
57 my @bits = split //, unpack 'B*', $msg;
58 my ($c, $n, @l) = (2, 0, 0, 0, 0);
63 if ($n > $l[$c]) { $l[$c] = $n; }
68 if ($n > $l[$c]) { $l[$c] = $n; }
69 ($c, $n) = ($l[0] > $l[1]) ? (1, $l[1]) : (0, $l[0]); # Take the smallest
71 @bits = (($c) x $n, 1 - $c, @bits, 1 - $c, ($c) x $n);
73 my $sig = ($_ == 0) ? SIGUSR1 : SIGUSR2;
83 Takes as its sole argument the callback triggered when a complete message is received, and returns two code references that should replace SIGUSR1 and SIGUSR2 signal handlers. Basically, you want to use it like this :
85 local @SIG{qw/USR1 USR2/} = mrecv sub { ... };
91 my ($bits, $state, $c, $n, $end) = ('', 0, undef, 0, undef);
92 my $sighandler = sub {
95 if ((substr $bits, -$n) eq $end) { # done
96 substr $bits, -$n, $n, '';
97 $cb->(pack 'B*', $bits);
99 } elsif ($state == 1) {
102 $end = (1 - $c) . $c x $n;
123 This module exports on request its two only functions, L</msend> and L</mrecv>.
127 use base qw/Exporter/;
130 our %EXPORT_TAGS = ( 'funcs' => [ qw/msend mrecv/ ] );
131 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
132 $EXPORT_TAGS{'all'} = \@EXPORT_OK;
136 L<POSIX> (standard since perl 5) and L<Time::HiRes> (standard since perl 5.7.3) are required.
140 L<perlipc> for information about signals.
142 For truely useful IPC, search for shared memory, pipes and semaphores.
146 Vincent Pit, C<< <perl at profvince.com> >>
148 You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
152 Please report any bugs or feature requests to
153 C<bug-ipc-morsesignals at rt.cpan.org>, or through the web interface at
154 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-MorseSignals>.
155 I will be notified, and then you'll automatically be notified of progress on
156 your bug as I make changes.
160 You can find documentation for this module with the perldoc command.
162 perldoc IPC::MorseSignals
164 =head1 ACKNOWLEDGEMENTS
166 Thanks for the inspiration, mofino ! I hope this module will fill all your IPC needs. :)
168 =head1 COPYRIGHT & LICENSE
170 Copyright 2007 Vincent Pit, all rights reserved.
172 This program is free software; you can redistribute it and/or modify it
173 under the same terms as Perl itself.
177 1; # End of IPC::MorseSignals