Posts tagged with: #perl

How to properly and immutably create a timestamp for a document (using Blockchain technology)

2025-02-03

I had written a Math paper which was not good enough to be published in a journal, yet I wanted to have proof of the date I wrote it on so I could post the paper on the web without other people later being able to question who first came up with the idea.

The way I thought was more correct to do this was to publish the paper's sha256 checksum on the Bitcoin blockchain. Posting the SHA on social media (such as x.com) was not enough, as paid members can edit their old posts.

I got in touch with BBRTJ, the maintainer of Bitcoin::Crypto, who was very helpful in teaching me how to use his module and achieving my aim.

The goal, he told me, was to include the SHA256 checksum as the comment (technically the 'NULLDATA') of a small bitcoin transaction.

  1. I created a BTC address with Perl, using Bitcoin::Crypto
  2. I transferred a very few dollars worth of BTC to this address
  3. I found the transaction ID at mempool
  4. I found the index number of the ouptut of the transaction that got crypto into my address, because this number together with the transaction ID are what form the UTXO
  5. I used this UTXO as input (funds) in a new transaction that had two outputs: One output was the NULLDATA string (the "comment" that contains the SHA256 checksum of the PDF document), and the other was a "change" address of mine, which would receive the rest of the money from the UTXO (full amount minus transaction fee (around $1)).

Here are these steps in more detail, and with code:

(click for more)

How to use perl v5.40's boolean builtins in Mojo::Pg queries

2024-07-13

Perl v5.40 introduced native true and false keywords. Unfortunately not all CPAN modules are ready to use them. One of those not yet ready is Mojo::Pg.

Normally you'd want to pass booleans to your queries as just 1's and 0's. However, since Mojo::JSON's true & false stringify to 1 and 0, my 5.38-using codebase is full of Mojo::Pg queries with Mojo::JSON's true and false as arguments.

This is a problem if I want to upgrade the perl interpreter of that project to Perl v5.40, because if I write "use v5.40;" in the file that contains those boolean keywords, Perl's builtin booleans will be used instead, which don't stringify to 1 and 0, but to 1 and the empty string, which can't be used by DBD::Pg in boolean fields and makes DBD::Pg throw an exception.

The solution I found was to subclass Mojo::Pg::Database, and wrap the query method, so that if Perl's builtin booleans are found, they are replaced in the query with native Pg booleans.

This is the subclass:

package Local::My::Pg::Database;

use v5.40;
use Mojo::Base 'Mojo::Pg::Database';

use DBD::Pg ':pg_types';

use experimental 'builtin';

sub query {
    my ($self, $query) = (shift, shift);
    my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
    my @args = map { builtin::is_bool($_) ? { type => PG_BOOL, value => $_ ? 1 : 0 } : $_ } @_;

    return $self->SUPER::query($query, @args, $cb // ());
}

...which requires the Mojo::Pg object to be told to use it:

$pg->database_class('Local::My::Pg::Database');

This solved my problem.

The other module I had problems with on Perl v5.40 is Mojolicious::Plugin::OpenAPI, which doesn't let the builtin booleans to be used in the API spec. I wrote a patch for that, and might submit it to the author.

Other than those, Mojo::JSON does not stringify builtin booleans to booleans, but to 1 and "" unless Cpanel::JSON::XS is installed, in which case it works fine.

Have you found other CPAN modules that need to be updated for Perl v5.40's booleans?

Today I learned... #1: variable scoping in if-else blocks

2024-07-04

This block of code is valid Perl:

if (my $var1 = calc1()) {
    say $var1;
} elsif (my $var2 = calc2()) {
    say "$var1, $var2";
}

As you can see, $var1, which is declared in the if clause, is visible inside the elsif clause too.

Perl never ceases to amaze me!

This is my new blog

2023-11-29

Welcome. As you will hopefully find out in this blog, Perl is the best language in the world.

#!/usr/bin/env perl

print "Hello, world!\n";