Skip to content

Instantly share code, notes, and snippets.

@dakkar
dakkar / format-tagged.pl
Created July 12, 2016 13:35
First stab at String::Formatter::Tagged
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
package String::Formatter::Tagged {
use parent 'String::Formatter';
use String::Tagged;
use Safe::Isa;
@dakkar
dakkar / sleep-alarm-die-eval.t
Last active October 12, 2016 16:06
A weird problem with sleep/alarm/die/eval
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
# this works in 5.8 and 5.24, fails under 5.18 5.20 and 5.22
sub do_it {
my ($call) = @_;
eval {
#!perl6
use v6.c;
class Thing {
method blah($val=1) { 15+$val }
}
class Thunk {
has $.maker;
@dakkar
dakkar / xscheck.pl
Created May 16, 2017 16:21
Check what C libraries get loaded when loading a Perl module
#!/usr/bin/env perl
use strict;
use warnings;
sub maps {
open my $fh, '<', '/proc/self/maps';
my @lines = <$fh>;
my %ret = map {
m{\ {10}(/.+)$} ? ($1,$_) : ()
} @lines;
@dakkar
dakkar / HelperThing.pm
Created October 26, 2017 09:38
Example of method traits in Moose
package HelperThing;
use strict;
use warnings;
use Moose::Exporter;
use 5.024;
use experimental 'signatures';
use MethodMetaRole;
sub my_method($meta,$name,@args) {
my $body = pop @args;
@dakkar
dakkar / auth-toy.p6
Created December 8, 2017 12:48
Simple authentication+authorisation example with Cro
use v6.d.PREVIEW;
use Cro::HTTP::Router;
use Cro::HTTP::Middleware;
use Cro::HTTP::Response;
use Cro::HTTP::Server;
use Cro::Transform;
# mixin for requests, carrying authentication info
role AuthToy::Request::Authed {
has Str $.user;
@dakkar
dakkar / tf.pl
Created May 9, 2018 15:12
weird interaction between `fork` and threads in perl 5 24.0
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
use threads;
sub run_child {
my ($value) = @_;
my $tid = threads->tid;
my @fizzes = (['','','Fizz'] xx *).flat;
my @buzzes = (['','','','','Buzz'] xx *).flat;
my @words = (@fizzes Z~ @buzzes);
my @numbers = 1 .. *;
my @fizzbuzz = (@numbers Z @words).map( *.max );
.say for @fizzbuzz[^20];
@dakkar
dakkar / foo.pl
Created May 22, 2018 15:14
alternative to FindBin::libs
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
my $libdir;
BEGIN {
my ($vol,$dirs,$file) = File::Spec->splitpath(__FILE__);
my @dirs = File::Spec->splitdir($dirs);
my $lib_dirs = File::Spec->catdir(@dirs,'lib');
$libdir = File::Spec->catpath($vol,$lib_dirs,'');
@dakkar
dakkar / nomatch.pl
Created June 12, 2018 14:49
how to check what caused a negative lookahead to fail a regex (i.e. what it did match)
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
use experimental 'signatures';
my @span=(-1,-1);
our $start=-1;
# this regex matches a series of -, some non-dashes that do not match
# 'some.{0,3}thing', and more dashes