Perlfunc: file_put_contents()

PHP has a really handy function called file_put_contents() that simplifies writing to a file. I did a quick Perl version of that function for my scripts.

sub file_put_contents {
    my ($file, $str) = @_;

    open (my $fh, ">", $file) or return undef;
    print $fh $str or return 0;
    close $fh;

    return length($str);
Leave A Reply

Apache: Redirect HTTP to HTTPs with exclusions

If you want to redirect all HTTP requests to HTTPS it's pretty simple:

<VirtualHost *:80>

    Redirect permanent /

I needed to do an HTTPS redirection, but exclude my /api directory to make things simpler and faster for my ESP8266/ESP32 boards. This is slighly more complicated but still doable:

<VirtualHost *:80>

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !/api
    RewriteRule /(.*)$1
Leave A Reply

Perl: Change a specific line in a file

If you need to change a specific line in a text file based on line number you can use the following Perl one-liner:

perl -pi -e '$_ = "New value\n" if $INPUT_LINE_NUMBER == 75' file.txt

Just change the 75 to whatever line number you want to update.

Leave A Reply

Javascript: Remove an item from an array

I need to remove a specific item from a Javascript array. I'm sure there are lot of ways to do it, but this is a quicky function I came up with to do it.

// Loop through an array removing any matching items
function remove_item_from_array(needle, haystack) {
    var ret = [];

    for (i in haystack) {
        var item = haystack[i];

        if (item !== needle) {

    return ret;
Leave A Reply

Perl: Fetch HTTPS content

If you need to fetch a remote URL via HTTPS in a Perl script the easiest way I have found is to use HTTP::Tiny. HTTP::Tiny is a core module, and included in all Perl installations.

Sample code:

use HTTP::Tiny;

my $http = HTTP::Tiny->new(verify_SSL => 1, timeout => 5);
my $resp = $http->get("");
my $body = $resp->{content};

print $body;
Leave A Reply

Fedora 31 major package versions

Fedora 31 has these versions of some core packages:

Package Version
Apache 2.4.41
GCC 9.2.1
Kernel 5.3.7
Perl 5.30.0
PHP 7.3.11
Vim 8.1.2198
Git 2.23.0
MariaDB 10.3.17
Leave A Reply

Perl: Printing lines of a file between two delimiters

I need to print out the lines of a text file that are between a specific starting delimiter and and ending delimiter:

perl -nE 'print if /START_DELIMITER/../END_DELIMITER/' input.txt

or use this to exclude the delimiter lines

perl -nE '/END_DELIMITER/ and $y = 0; $y and print; /START_DELIMITER/ and $y = 1' input.txt

This method also works for data passed in via a pipe.

Leave A Reply

Linux: CentOS 8 major package versions

CentOS 8 has these versions of some core packages:

Package Version
Apache 2.4.37
GCC 8.2.1
Kernel 4.18.0
Perl 5.26.3
PHP 7.2.11
Vim 8.0.1763
Git 2.18.1
Leave A Reply

ZSTD compression level with tar

I've been using zstd more and more for general compression. Using it with tar is pretty straight forward:

tar -cvpf -I zstd /tmp/etc.tar.gz /etc

This does not allow you to set the compression ratio however. After some digging I found an environment variable that controls the default compression level. If you run tar like this you can change the default compression level.

ZSTD_CLEVEL=19 tar -cvpf -I zstd /tmp/etc.tar.gz /etc

GZip has a similar variable, but it will allow any command line variable (not just compression ratio).

GZIP=-9 tar -cvzpf /tmp/etc.tar.gz /etc
Leave A Reply

Restricting SSH sessions to a single command

Using SSH keys to allow passwordless logins to a server allows cool scripting opportunities. You may want to allow a user to login via SSH, but restrict them to a specific command. This can be done with a command option in your authorized_keys file. If you prepend a command="" option to your SSH key line, SSH will force that command to run that command every time a user with that key connects.

command="/usr/bin/ls /etc -lsa" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDunzvfGJi4qcHixaoCQ0k1kociSYfIlwhXzI1ywtltA user@desktop
Leave A Reply

Javascript: Get a unixtime value

I need a Unixtime in Javascript. This is the simplest way I came up with to get that value:

var unixtime = parseInt(new Date().getTime() / 1000);
Leave A Reply

HTML5 + CSS toggle slides

I was looking for a mobile friendly slide toggle but not having much luck. There a lot of options but I want something flexible, something I could modify with JavaScript, and something that is HTML form compliant. I ended up modifying this W3C example and bundling it in a CSS file. It uses CSS calc() and CSS variables so it requires a semi-modern browser.

SlideToggle demo

Leave A Reply

Git: Show all commits that affect a certain line

I found a bug on a particular line, and need to track down the history for that line. This is easy enough with git blame, but in this case I'm getting false positives. There was a previous check-in that changed whitespace on that line. Obviously that was not the commit that introduced the bug.

Using git log with the -L flag you can specify a file and and range of lines to search through.

git log -L 57,57:index.php

This will show all the commits in index.php where there was a change on line #57.

See also: Finding commits that match a search term

Leave A Reply

How Lovecraft's Old One's drive people mad

Reddit user thomascgalvin has an amazing summary of why encountering one of Lovecraft's Old Ones would almost instantly drive you mad.

When you were a child, you were probably taught that you have five senses: sight, sound, touch, taste, and smell. This is true, but it's incomplete.

You have plenty of other senses. A sense of balance. A sense of time passing. A sense of changing temperatures. A sense of where your body is in space. A sense of acceleration. A sense of pain. A sense of loss. A sense of joy. A sense of agency. A sense of belonging.

You perceive the world around you in a dozen different, subtle ways, many of which you can't even name, and most of which you are completely unaware of, unless something goes wrong.

The right (or wrong) stimulus can completely change our psychology. When someone sees the Grand Canyon for the first time, they often report a feeling of overwhelming awe at the sheer size and spectacle. When astronauts return from space, return from seeing this great world of ours reduced to a tiny blue marble out the window, the report a sense of overwhelming concern for, and oneness with, humanity. When someone takes psilocybin or DMT, they report an expansion of their mind that fundamentally alters who they are as a person.

But what about people who have seen the horrors of war? People who have suffered at the hands of an abusive lover? Or people who have suffered something as simple as a car crash, and now break out in a cold sweat every time they drive? The wrong stimulus, applied to our normal, human senses, can irreparable harm us.

When someone sees a Great Old One, the sheer monstrous nature of it, the impossibility, the implacability, the inhuman and inhumane otherness of the thing that they have laid eyes on, is enough to shatter, completely and forever, their sense of who and what and why they are. It's like looking at the splendor of the Grand Canyon and realizing that it will absolutely destroy you, not because it hates you, not because it needs to, but because that is simply what it is.

Just as you were unaware of the fact that you aren't falling until I just mentioned it, you are not aware of the fact that you believe that you are important, that you matter, that you have any hope at all of making a difference in this vast and careless universe until something forces you to confront that fact. And that is what the Great Old Ones do. When you look upon them, you are looking at a power so vast and ancient and amoral that any sense of normalcy you once had will be forever torn away.

And that's just what we can explain with out current understanding of the human mind. Who's to say that we don't have some other sense, a sense of the cosmic, a sense of the psychic, that is triggered by the presence of a Great Old One? Who's to say that some sense, long dormant in the safety of our comfortable little world, isn't woken up as soon as one of those eldritch abominations appears before us?

Leave A Reply

Perl: Open a serial port at a given speed

I needed to open a serial port to read and write data from my Arduino. The after much digging I was able to come up with the following function to get a file handle for a given serial port at a specific speed.

use Fcntl qw(O_RDWR);

# Open a serial port at a given speed and return a filehandle
sub open_serial_port {
    my $dev   = shift();
    my $speed = shift();

    sysopen(my $fh, $dev, O_RDWR) or die("Unable to open $dev\n");

    # Figure out the integer that maps to a given speed
    # perl -E 'use IO::Tty qw( B115200 ); say B115200'
    my $speed_map = {
        9600   => 13,
        19200  => 14,
        38400  => 15,
        57600  => 4097,
        115200 => 4098,

    my $speed_int = $speed_map->{$speed};
    if (!$speed_int) {
        die("Unable to find speed $speed\n");

    # Set the baud on the FH
    my $attr = POSIX::Termios->new;


    return $fh;
Leave A Reply