Skip to content

Instantly share code, notes, and snippets.

@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 $@;
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 / 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) = @_;
(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 / 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.
# $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 / hack.sh
Created February 7, 2016 00:26 — forked from aki-null/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2261536/hack.sh | sh
#
@jhthorsen
jhthorsen / nytprofmojo.sh
Last active February 19, 2016 11:44
NYTProf for Mojolicious
#!/bin/sh
# nytprofmojo.sh <time> <port>
# nytprofmojo.sh 10s 8080
MOJO_PORT=${2:-3456}
perl -d:NYTProf -Ilib examples/hello.pl daemon -l http://*:$MOJO_PORT -m production 2>stderr.log &
MOJO_PID=$!
sleep 1
wrk -c 100 -d ${1:-10} http://localhost:$MOJO_PORT
kill $MOJO_PID
#!/usr/bin/env perl
use Mojolicious::Lite;
use List::Util 'reduce';
use Time::Piece;
use Time::Seconds;
sub off {
return (
'01-01' => 'Nyttårsdag',
'04-02' => 'Skjærtorsdag',
@jhthorsen
jhthorsen / void.pm
Last active September 30, 2016 11:48
This is stupid, but I made it anyway
package void;
use strict;
use Exporter 'import';
use Carp;
our @EXPORT = 'void';
sub void {
return unless defined wantarray;
my $caller = (caller 1)[3];