Skip to content

Instantly share code, notes, and snippets.

Joel Berger jberger

Block or report user

Report or block jberger

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View socks
#!/bin/bash
PID=$(pgrep -f 'ssh -D')
CMD=$1
if [[ $CMD == '--stop' ]]
then
if [[ ! "$PID" ]]
then
echo 'socks proxy is not running';
View ovid.pl
use Mojo::Base -strict, -signatures;
use Mojolicious;
use Mojo::IOLoop;
use Mojo::Server::Daemon;
use Mojo::UserAgent;
# mock external websocket application
# waits 5s after connection then sends time and closes
my $external = Mojolicious->new;
View handled.diff
diff --git a/lib/Mojo/Promise.pm b/lib/Mojo/Promise.pm
index 0c66018db..db09a0033 100644
--- a/lib/Mojo/Promise.pm
+++ b/lib/Mojo/Promise.pm
@@ -185,6 +185,7 @@ sub _defer {
my $cbs = $self->{status} eq 'resolve' ? $self->{resolve} : $self->{reject};
@{$self}{qw(cycle resolve reject)} = (undef, [], []);
+ $self->{handled} = 1 if $self->{status} eq 'reject' && @{ $cbs || [] };
$self->ioloop->next_tick(sub { $_->(@$result) for @$cbs });
View gist:2a4387dcebdb8c155677797367cb4b96
$ git diff
diff --git a/t/mojo/promise_async_await.t b/t/mojo/promise_async_await.t
index 049253e47..084ad8de3 100644
--- a/t/mojo/promise_async_await.t
+++ b/t/mojo/promise_async_await.t
@@ -79,6 +79,13 @@ async sub test_two {
return $text;
}
+async sub returns_promise {
View test.pl
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
#app->defaults({text => 'Hello World'});
#get '/';
#get '/' => { text => 'Hello World' };
get '/' => sub ($c) {
View example.pl
my ($time, $ticket_ids) = await Mojo::Promise->all(
$redis->get_p("department:$id:tickets:refresh"),
$redis->smembers_p("department:$id:tickets"),
);
return [] unless @{ $ticket_ids->[0] };
my @tickets = await Mojo::Promise->all(map { $self->get($_) } @{ $ticket_ids->[0] });
return (
[map { $_->[0] } @tickets],
$time->[0],
@jberger
jberger / gist:9db7e7711d46d06dd013fcf4b74ee689
Created Dec 5, 2019
Quick discussion about async/await
View gist:9db7e7711d46d06dd013fcf4b74ee689
<jberger>async/await is going to take over the world
its going to be the next "OO is everything, if it isn't OO it is nothing"
"oh your language doesn't have a/a, ok boomer"
suddenly I see the lack of core a/a in perl(5) as as much of an existential threat as the confusion over perl6 was
<mishanti1>The competition we have is not the lack of feature A or B, but simply agility in introducing new features in the language.
<jberger>generally I agree with you, but this one is a big deal
it is changing how students are being taught to code
it is changing how apis are being designed
and younger coders literally aren't going to think in callbacks, nor even in promise chains really, in the near future
the rare actual paradigm shift
View test.pl
#!/usr/bin/env perl
use strict;
use warnings;
use YAML::PP;
my $yaml = YAML::PP->new( schema => ['YAML1_1'] );
# or
#use YAML::PP::LibYAML;
View log_context.pl
use Mojolicious::Lite -signatures;
sub _generate_id { ... }
{
package Mojo::Log::WithContext;
use Mojo::Base 'Mojo::Log', -signatures;
has context => sub { {} };
has id => sub { _generate_id() };
has format => sub { sub ($id, $first, @lines) { ("[$id] $first", @lines) } };
View jeopardy.pl
use Mojo::Base -strict;
use Mojo::URL;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my ($id) = @ARGV;
my $url = Mojo::URL->new('http://www.j-archive.com/showgame.php')->query(game_id => $id);
You can’t perform that action at this time.