How to prevent an infinite loop

This loop (assuming you have an /etc/passwd and may read it) runs forever:

while () {
  open my $fh, '<:unix', '/etc/passwd' or die $!;

This loop terminates:

while () {
  open my $fh, '<', '/etc/passwd' or die $!;
  binmode $fh, ':unix';

Note that you will have to live with some extraneous output:
Too many open files at line 2.

Btw, you can make it loop forever again this way:

require POSIX;
while () {
  open my $fh, '<', '/etc/passwd' or die $!;
  binmode $fh, ':unix';
  POSIX::close fileno $fh;

Good bye PrePAN

The domain was snapped up by a squatter sometime between July and August. 🙁 What a pity, I always enjoyed those conversations.

On interpolating stuff into pattern matches

Tom Wyant:

Interestingly (to me, at least) they reported that the removal of the /o modifier made their case 2-3 times slower. This surprised me somewhat, as I had understood that modern Perls (for some value of "modern") had done things to minimize the performance difference between the presence and absence of /o.

They indeed have.

Ironically, it’s qr objects which don’t get that benefit. On the machine I’m typing on, the following benchmark…

The ordering operators

Perl has two operators, cmp and <=>, which are basically never seen outside of sort blocks.

That doesn’t mean you can’t use them elsewhere, though. Certainly sort and these operators were designed to work seamlessly together but there isn’t anything sort-specific about the operators per se, and in some contexts they can be the most appropriate solution.

An example would be some code that used to go (almost exactly) like this:

my ( $maj, $min, $rel ) = split /[.]/, $version;
my $version_ok = $maj > 3 || (
    $maj == 3 && ($min > 6 || (
        $min == 6 && $rel >= 8

Basically, this is a check that the version number of something with a three-component dotted version is at least 3.6.8. As expressed in the code, for both the major and minor version there are two cases where they might be accepted: if the component is above a given threshold then the whole version is acceptable with no further questions, but if equal to that threshold, the version might still be acceptable depending on the value(s) of the lesser component(s). At the same time, if the value is below that threshold, the version is unacceptable, no further questions necessary. So there are three cases, two of which can succeed, and two of which require no further checks.

This is exactly what the <=> operator offers: if you ask $maj <=> 3 you will get 1 if the major version is above 3 (immediate success), -1 if it is below 3 (immediate failure), and 0 if it is equal to 3 (success to be determined from the lesser components). Because 0 is false, you can chain such comparisons with logical-or, until one of them gives an immediate success/fail – as of course we are used to doing in sort blocks.

And so this is what the code looks like now:

my ( $maj, $min, $rel ) = split /[.]/, $version;
my $version_ok = ( $maj <=> 3 || $min <=> 6 || $rel <=> 8 ) != -1;

The expression in parentheses has each component of the actual version on the left and the respective component of version 3.6.8 on the right, and will stop at the first comparison that gives a definite answer. The final != -1 then checks that the answer was not “the right side is greater” at the final comparison – i.e. “the given version is not below version 3.6.8”.

To whom this MySQL UTF-8 news may concern

Strictly speaking not news exactly, given that it dates from early 2018, but it was news to me, and since I haven’t seen it make the rounds I still find it worth disseminating. From the MySQL 8.0.11 release notes:

The utf8mb3 character set will be replaced by utf8mb4 in a future MySQL version. The utf8 character set is currently an alias for utf8mb3, but will at that point become a reference to utf8mb4. To avoid ambiguity about the meaning of utf8, consider specifying utf8mb4 explicitly for character set references instead of utf8.

This a declaration of intent rather than a change that has happened… but still. It’s been a very long September

(To prepare for this you’ll want to note what you need to take into account regarding the way utf8mb4 and indexes interact.)