X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=slides.html;h=51f91018b47b58dc661ea16ec0f31ef6e68920f0;hb=309c1da6df9319b3bcd760fd3073bafeda9faf09;hp=d86ee03aae1a18331b5ee606173fcdcf85011bb0;hpb=55d3713208b2267dfbb669f76a9a21f4ff6092d5;p=chaz%2Ftalk-event-driven-programming-in-perl diff --git a/slides.html b/slides.html index d86ee03..51f9101 100644 --- a/slides.html +++ b/slides.html @@ -445,6 +445,89 @@ while (1) { - [`Mojo::Reactor::Poll`](https://metacpan.org/source/Mojo::Reactor::Poll) ] +--- +name: not-all-roses +class: center, middle + +![Thorns](img/thorn.jpg) + +## Watch out for the thorns... + +??? +There are some special considerations you need to take when writing event-driven code. + +--- +class: center, middle + +## Exceptions for error handling + +--- +class: center, middle + +### Problem: No exception handler up the call stack + +--- +class: ex-exceptions + +## Rule: Don't die/throw in event handlers. + +-- +### Error callback pattern + +```perl +do_something_asynchronously( + callback => sub { ... }, + on_error => sub { ... }, +); +``` + +--- +class: ex-exceptions + +## Rule: Don't die/throw in event handlers. + +### Use promises + +```perl +my $promise = do_something_asynchronously(); + +$promise->on_done(sub { ... }); +$promise->on_fail(sub { ... }); +``` + +--- +class: center, middle + +## `SIGPIPE` + +--- +class: sigpipe +## `SIGPIPE` + +- Sent to your program when it writes to a pipe that was closed. + +-- +- Default signal handler terminates the program. + +--- +class: ex-sigpipe + +## Solution: Ignore `SIGPIPE` + +```perl +$SIG{PIPE} = 'IGNORE'; +``` + +??? +Some event loops do this for you. + +-- +.big[ +Look for `EPIPE` from syscalls (like [`write`](http://man.he.net/man2/write)) instead. + +(You *are* checking return codes from your system calls... right?) +] + --- class: center, middle