Using variables in Keyboard Maestro scripts

Having fallen in love with Keyboard Maestro for its flexibility in macOS automation, I began experimenting with scripting in various languages, like my old favourite Perl. That’s when the fun began. How do we access KM variables inside a Perl script.

Let’s see what the documentation says:

So the documentation clearly states that this script

#!/usr/bin/perl

print scalar reverse $KMVAR_MyVar;

should work if I have a KM variable named MyVar. But, you guessed it - it does not.

After digging around in the Keyboard Masestro forums, I found an obscure post that pointed the way. It turns out that the Perl access to KM variables is completely different from what the documentation claims, the format is not $KMVAR_MyVar, it is actually:

$ENV{KMVAR_MyVar}

so the above script works if the variable is accessed from Perl that way:

#!/usr/bin/perl

print scalar reverse $ENV{KMVAR_MyVar};