Skip to content

Instantly share code, notes, and snippets.

# $self->query_to_where({name => "Bruce"}, {name => "eq"}) => ["name = ?"], ["Bruce"]
# $self->query_to_where({name => "Bruce"}, {name => "like"}) => ["name LIKE '%?%'"], ["Bruce"]
# $self->query_to_where({age => ">=42"}, {age => "num"}) => ["name >= ?"], [42]
sub query_to_where {
my ($self, $q, $rules) = @_;
my (@where, @bind);
for my $col (sort keys %$rules) {
if (!length $q->{$col} // '') {
1; # next
@jhthorsen
jhthorsen / Cursor.pm
Last active October 20, 2015 18:08
Mojo::Cursor - General purpose async iterator
package Mojo::Cursor;
=head1 NAME
Mojo::Cursor - General purpose async iterator
=head1 DESCRIPTION
L<Mojo::Cursor> is a class for instantiating an iterator object.
(function() {
/*
var swagger = new SwaggerClient({swagger: "2.0", info: {}, paths: {}});
swagger.listPets({limit: 10}, function(err, xhr) {
console.log(xhr.code, xhr.body);
});
*/
window.SwaggerClient = function(spec) {
var self = this;
@jhthorsen
jhthorsen / stdinping.pl
Created February 20, 2015 11:05
This program can read a list of IP's from STDIN and ping them non-blocking
#!/usr/bin/perl
# Usage: echo -en "1.1.0.1\n127.0.0.1\n" | sudo /usr/bin/perl stdinping
# A lot of this code is mostly copy/paste/modify from Net::Ping
BEGIN {
# Make sure we don't load code that is not owned by root
$^X eq "/usr/bin/perl" or die "Invalid perl: $^X\n";
my @perl_stat = stat $^X;
unshift @INC, sub {
my ($code, $module) = @_;
package Mojo::IOLoop::Tail;
use Mojo::Base "Mojo::IOLoop::Stream";
has check_timeout => 0.01;
sub close {
my $self = shift;
return unless my $handle = $self->{handle};
seek $handle, 0, 1;
@jhthorsen
jhthorsen / getpeereid.pl
Created November 7, 2014 08:57
This example show how to get the peer username from a unix socket. (getpeereid SO_PEERCRED AF_UNIX)
# See also
# http://www.perlmonks.org/bare/?node_id=881003
# http://www.freebsd.org/cgi/man.cgi?query=getpeereid
use strict;
use warnings;
use IO::Socket::UNIX;
use Socket qw( SO_PEERCRED SOL_SOCKET );
if (@ARGV) {
IO::Socket::UNIX->new(Peer => $ARGV[0], Type => SOCK_STREAM, Timeout => 10) or die $@;
plugin 'OAuth2', {
google => {
key => 'public app id from google',
secret => 'private secret from google',
},
};
@jhthorsen
jhthorsen / dom-events.js
Last active January 8, 2022 21:06
emit, on, once in pure javascript
Object.prototype.emit = function(name, d) {
this.dispatchEvent(new CustomEvent(name, { detail: d }));
return this;
};
Object.prototype.on = function(name, cb) {
this.addEventListener(name, cb, false);
return cb;
};
Object.prototype.once = function(name, cb) {
var wrapper = function() {
@jhthorsen
jhthorsen / watch-port.sh
Last active May 13, 2022 01:04
Fix iwlwifi power save
#!/bin/sh
EXPRESSION=${1:-"tcp port 22"};
DEVICE="wlan0";
while tcpdump -p -c 1 -i $DEVICE "$EXPRESSION"; do
iwconfig $DEVICE power off;
sleep 600;
iwconfig $DEVICE power on;
done
@jhthorsen
jhthorsen / mojo.log
Created July 24, 2014 12:44
make a new mojolicious dist #mojo
git remote update
git rebase origin/master
# no diff
git tag v5.17
perl Makefile.PL; make manifest; make dist
1) make sure master is in a good state
2) tag release
3) perl Makefile.PL; make manifest; make dist
4) upload release, ...optional...