perl

A 7-post collection

Hash Keys Manipulations

Atoms, strings and numbers are three different things. But when type coercion enters the game, some languages can be confusing. Elixir: Atoms And Strings Unsurprisingly this map: %{foo: 10} is not the same as this map: %{"foo" => 10} This of course is easy to see here, but when maps are not defined in code it can be confusing. For example this code: input = IO.stream(:stdio, :line) |> Enum.at(0) |> String.trim Map.get(%{p: "Hello"}, input) |> IO.

Read more

Insights from Advent Of Code (Week 1)

My first week solving advent of code has almost ended, which makes it a good time to reflect on the quizzes, my solutions and other solutions I found online. tl;dr Advent of Code is a series of coding quizzes delivered daily during December. Project website is: https://adventofcode.com AOC turned out to be a very nice way to learn a new programming language (I'm learning Elixir). Most puzzles are very easy, as seen in the leaderboard times. Practising a short coding quiz every day

Read more

Solved: Mojolicious Websockets Inactivity timeout

A recent snippet of Mojolicious webapp I recently wrote would not allow browsers to connect and upgrade to websockets. Below you'll find the code and explanation so hopefully it'll save you some debugging time. Bug Symptoms Connecting to the websocket endpoint gave me the following error in the browser console: WebSocket connection to 'ws://localhost:3000/feed/news' failed: Connection closed before receiving a handshake response (anonymous) @ update:18 And at the same time in the server console: [debug] Inactivity timeout So it looks like the

Read more

Still Slow After Fork

A simple paradigm for speeding up code is to use multiple processes created by fork. Unfortunately this doesn't always work as we can see in the following example. Calculating Tons of MD5 A simple case would be brute forcing MD5. Using Math::Cartesian::Product it's easy to generate all the 5 letter combinations and calculate an MD5 sum from each of them. If we were searching for the sources of the following MD5s: 5d41402abc4b2a76b9719d911017c592 3899dcbab79f92af727c2190bbd8abc5 9aeaed51f2b0f6680c4ed4b07fb1a83c 96e25e3c6373262faae824dc5a16cce1 We could use the following perl program to brute

Read more

Fixing Legacy Perl Functions With Decorators

Function decorators give us a way to modify a function's behaviour without changing its source. This is useful in changing the behaviour of complex or legacy functions that you don't want to touch. Although perl does not have a built-in syntax for creating or using decorators, typeglob manipulations are used to easily fill the gap. Reasons To Decorate Take the following implementation for fibonacci calculating function: sub fib { my ($n) = @_; print "DEBUG - calculating fib($n)\n"; $n < 2 ? 1 : fib($n-1) + fib(

Read more