Skip to content

Instantly share code, notes, and snippets.

Avatar

Hauke D haukex

View GitHub Profile
@haukex
haukex / pm_ajaxcb.js
Last active Jun 9, 2020
PerlMonks Browser Notifications for New Nodes
View pm_ajaxcb.js
"use strict";
// Documented at: https://www.perlmonks.org/?node_id=11116369
$(function () {
var CB_FETCHTIME_MS = 15 * 1000;
var GERNERAL_REFRESHTIME_MS = 60 * 1000;
// AJAX CB Refresh
var refresh = function () {
$.ajax({ url: '/', dataType: 'html',
// testing shows the table rendered here is the same as in the nodelet
data: { node: 'showchatmessages', displaytype: 'raw', ticker: 'yes' } })
View cpanfile
# cpanm --installdeps .
requires 'Mojo::DOM';
requires 'HTML::TreeBuilder::XPath';
requires 'HTML::LinkExtor';
requires 'WWW::Mechanize', '1.97';
requires 'XML::LibXML';
# for runsols.pl:
requires 'Path::Class';
requires 'IPC::Run3';
requires 'Test::More';
View 1229406.pl
#!/usr/bin/env perl
use warnings;
use strict;
my %tehcodez = (
sortfirst => q{
@list = sort {$a<=>$b} @list;
@list = ( (grep {$_>=0} @list), (grep {$_<0} @list) ); },
grepfirst => q{
my @pos = grep {$_>=0} @list;
@haukex
haukex / conf.py
Created Dec 10, 2018
Python: pathnames relative to current python file
View conf.py
import os
my_dir = os.path.dirname(os.path.abspath(__file__))
sub_dir = os.path.join(my_dir, 'test');
up_dir = os.path.join(os.path.split(my_dir)[0], 'other');
View re.pl
use warnings;
use 5.018;
# run this multiple times: sometimes it prints "x1x2x1x", sometimes "x2bar-x2x2bar-x"
{
my %repl = ( '-foo-bar-'=>1, '-foo-'=>2 );
my $re = join '|', map { "\\b$_\\b" } keys %repl;
$re = qr/$re/;
View benchmark.txt
# WARNING # WARNING # WARNING # the following command generates a ~1 GIGABYTE file!
$ perl -le '$,=$/;for(1..50000){print+("Foo")x5000,"Bar CAPTURE Bar"}print+("Foo")x5000' >test.txt
# For testing outputs:
# perl -le '$,=$/;print"Bar CAPTURE Bar";for(1..49999){print+("Foo")x5000,"Bar CAPTURE Bar"}' >out_expect.txt
# UPDATE: Replace "time" with "/usr/bin/time" to get memory usage stats as well!
View gaps.pl
#!/usr/bin/env perl
use warnings;
use strict;
use Time::HiRes qw/gettimeofday tv_interval/;
use DBI;
my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:",undef,undef,
{ RaiseError=>1, AutoCommit=>0 } );
{ # create table and fill with fake data
$dbh->do(<<'END_SQL');
View lookup_bench.pl
#!/usr/bin/env perl
use warnings;
use strict;
use Benchmark 'cmpthese';
use List::Util 'first';
my @tests = (
{ nrand=> 100, nlook=> 10, match=>'beginning' },
{ nrand=> 100, nlook=> 100, match=>'beginning' },
{ nrand=>10_000, nlook=> 100, match=>'beginning' },
View dumptiefail.pl
use warnings;
use strict;
use Data::Dumper;
use Memoize::Expire;
tie my %cache => 'Memoize::Expire';
my $self = { cache => [ \%cache ] }; # self is not tied nor an object
eval { print Dumper($self); 1 } or warn $@;
#=> Can't locate object method "FIRSTKEY" via package "Memoize::Expire" at ...
View regbench.pl
use warnings;
use strict;
use Benchmark qw/cmpthese/;
for my $r (10,100,1_000,10_000) {
print " ___ $r ___\n";
my $str = "foo;bar;" x $r;
cmpthese(-1, {
orig => sub { # https://stackoverflow.com/q/48961457/9300627
$str=~s/\b([^;]+)(?=.*;\1;);//gr;