[Pdx-pm] Apache, mod_perl, Catalyst, qx, and the ampersand
Wil Cooley
wcooley at nakedape.cc
Thu Dec 21 11:29:53 PST 2006
On Wed, 2006-12-20 at 19:39 -0500, Michael G Schwern wrote:
> For those of use who never can remember which side of the if/else is
> the parent and which is the child and the idea of two conditions of a
> branch running simultaneously makes their brain melt and having all
> that forking housekeeping code scattered around, I like to do
> something like this....
>
> sub worker (&) {
> my $worker = shift;
>
> if( my $pid = fork ) { # parent
> ...do whatever housekeeping the parent needs to do...
> }
> elsif( defined $pid ) { # child
> ...do whatever housekeeping the child needs to do...
> $worker->();
> ...cleanup...
> }
> else {
> ...fork error...
> }
> }
>
> Then just call it like so:
>
> worker {
> ...child code...
> };
/me blinks. Been learning Ruby?
Not that Ruby has a monopoly on this sort of thing, but I never really
saw the full potential until I learned Ruby. The only problem that
makes the syntactic sugar less sweet is that the block-as-anon-sub is
limited to the first parameter, so it doesn't mix as well with OO
because you have to use a proper anonymous-sub.
I.e.,
package MyClass;
sub each($&) {
my ($self, $sub) = @_;
for my $item ($self->get_items()) {
$sub->($item);
}
}
__END__
Then this works:
$collection->each(sub {
...
});
But this still doesn't:
$collection->each {
...
};
Wil
--
Wil Cooley <wcooley at nakedape.cc>
http://nakedape.cc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://mail.pm.org/pipermail/pdx-pm-list/attachments/20061221/1257e5e6/attachment.bin
More information about the Pdx-pm-list
mailing list