My tech editor recently pointed out something that I found quite astonishing about the Win32::TieRegistry. If you are not familiar with this extension, it is quite useful: by tie-ing a hash to the Windows registry it is quite simple and easy to access registry hives, keys, values and data–as easy as using a simple Perl hash.

This extension is a Godsend for everyone who has have never programmed to the Win32 registry APIs or used Perl’s Win32::Registry (which pretty much mirrors the Win32 registry APIs). The registry APIs are not the most intuitive, even though they are logical (once you bother to learn them).

But what amazed me was that Win32::TieRegistry makes some pretty vast assumptions about permissions. As he explained to me, when you load the extension in your script it attempts to open the registry with read and write privileges. This is fine if you are running a script as an administrator or only accessing keys that you have been granted such permissions. But if you are not an elevated user then attempts to only read keys, values and data will fail — without tripping a fault or some kind of noticeable error.

Wow, that is a pretty colossal problem. Luckily my editor offered up some code that, if run in a BEGIN block, mitigates the problem by only requesting the maximum permissions that your user account is allowed:

BEGIN
{
   use Win32::TieRegistry( Delimiter=>"\\", ArrayValues=>0 );
   $Registry = $Registry->Open("", {Access => 0×2000000});
}

 

Seriously, wow. You would think that open source code would have had such a big problem patched long, long ago.