Skip to content

Instantly share code, notes, and snippets.

View tobyink's full-sized avatar
🦈
hi

Toby Inkster tobyink

🦈
hi
View GitHub Profile
@tobyink
tobyink / .pl
Last active August 4, 2023 14:47 — forked from ology/.pl
Benchmark any vs grep
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark;
use List::Util 'any';
use match::simple::XS;
use match::simple qw(match);
my $count = shift || 2_000_000;
@tobyink
tobyink / perl-print.pl
Created March 23, 2023 13:29
Demonstration of some Perl punctuation variables which affect `print`
use strict;
use warnings;
{
local $\ = "\n"; # output record separator
local $| = 1; # autoflush
print "Hello";
print "World";
}
sub import {
shift;
@_ = 'with' unless @_;
Keyword::Simple::define $_, \&__rewrite_with for @_;
}
sub unimport {
shift;
@_ = 'with' unless @_;
Keyword::Simple::undefine $_ for @_;
@tobyink
tobyink / get-weather.pl
Created February 20, 2023 23:33
Perl HTTP+JSON example
use v5.16;
use HTTP::Tiny;
use JSON::PP 'decode_json';
my ( $lat, $lon ) = ( 54.5, -1.55 );
my $url = sprintf(
'https://api.open-meteo.com/v1/forecast?latitude=%s&longitude=%s'.
'&daily=temperature_2m_max,temperature_2m_min&timezone=Europe/London',
$lat,
$lon,
@tobyink
tobyink / monty.pl
Last active January 25, 2023 11:50
Monty Hall
package MySim;
use Zydeco;
use List::Util qw( shuffle sample );
# Next line shouldn't be needed, but I guess a bug somewhere in Zydeco...
BEGIN { package MySim::Types; use Type::Library -base; }
class Door {
param prize ( type => Bool, default => false, is => ro );
@tobyink
tobyink / example.pl
Created January 2, 2023 14:07
:scalar, :list, and :void
use v5.40;
use Local::Animal;
sub default_species :scalar {
return "Guinea Pig";
}
my $geoffrey = Local::Animal->new(
name => "Geoffrey",
@tobyink
tobyink / fortune-500.pl
Created October 25, 2021 09:40
Calculate the excess profits of Fortune 500 companies and how much of a pay rise they could give employees
#!perl
use strict;
use warnings;
use constant {
THOUSAND => 1_000,
MILLION => 1_000_000,
BILLION => 1_000_000_000,
};
@tobyink
tobyink / hue-mood.pl
Created January 22, 2021 08:40
Cycles a list of Philips Hue light bulbs through a gradient of colours
#!/usr/bin/env perl
use v5.16;
use warnings;
package ColourSet {
use Moo;
use Types::Standard -types;
has initial => (
@tobyink
tobyink / cpanm-5.6.pl
Created November 23, 2020 18:07
App::cpanminus for Perl 5.6
#!/usr/bin/env perl
#
# This is a pre-compiled source code for the cpanm (cpanminus) program.
# For more details about how to install cpanm, go to the following URL:
#
# https://github.com/miyagawa/cpanminus
#
# Quickstart: Run the following command and it will install itself for
# you. You might want to run it as a root with sudo if you want to install
# to places like /usr/local/bin.
@tobyink
tobyink / my-perltidy
Last active November 19, 2020 22:18
tobyink's custom perltidy
#!/usr/bin/env perl
use strict;
use warnings;
use Perl::Tidy;
exit Perl::Tidy::perltidy(
perltidyrc => \( do { local $/ = <DATA> } ),
prefilter => sub {
no warnings;