3

我有一个可以获取注册表项值的脚本。这是代码。

    use strict;
    use warnings;

    my $winRegistryStatus=0;

    eval {
       require Win32::Registry;
       Win32::Registry->import();
    };
    unless($@) {
       $winRegistryStatus=1;
    }

    my $registryPath = "Self\Random";
    my $keyName = "Configure";
    my $registryKeySettings;
    my %registrySubKeyValues;

    $main::HKEY_LOCAL_MACHINE->Open($registryPath, $registryKeySettings) || die "Cannot open $registryPath: $!";
    $registryKeySettings->GetValues(\%registrySubKeyValues); # get sub keys and value -hash ref
    foreach my $subKey (keys %registrySubKeyValues) {
        my $_subKey = $registrySubKeyValues{$subKey};
        next unless $$_subKey[0] eq $keyName;
        print "Configure=" . $$_subKey[2];
    }

输出

   Name "main::HKEY_LOCAL_MACHINE" used only once: possible typo at ....
   Configure=Yes

我可以获得 Configure 的值,但它也会返回一个我不知道如何修复它的警告。

任何机构我哪里错了,可以告诉我如何解决它?

谢谢。

4

2 回答 2

4

“仅使用一次”是一个警告,use warnings因为您只使用$main::HKEY_LOCAL_MACHINE过一次。你在这里没有错。这只是暗示您可能忘记了某些东西。

在这种情况下,您可以忽略它,或者简单地停用这种警告:没有警告“一次”。

一般来说,将这些内容包含在 a 中BLOCK并添加一个长长的描述性注释来解释为什么您在此处关闭这种警告是一个好主意。

{ # Disable 'used only once' warning because the $::HKEY_...
  # var was imported by Win32::Registry and is not used anywhere else.
  $main::HKEY_LOCAL_MACHINE->Open($registryPath, $registryKeySettings) 
    || die "Cannot open $registryPath: $!";
}

warnings 您可以在此处找到更多信息。

于 2012-07-23T08:28:21.893 回答
2

这个模块相当奇怪,因为它将符号导出到main包中,而不管它是从哪里使用的。

但在你的情况下,这就是你想要的:你的程序在main你没有package声明的情况下,你可以省略main::from $HKEY_LOCAL_MACHINE

至于您的问题,您显示的代码不会引发您所说的警告。问题一定出在其他地方。请您出示您的完整代码,以便我们更好地为您提供建议。

同时,请注意@Sinan Ünür 的建议 -Win32::TieRegistryWin32::Registry. 甚至 POD 文档也Win32::Registry这样说:

注意:这个模块提供了一个非常笨拙的界面来访问 Windows 注册表,目前还没有积极开发。它的存在只是为了向后兼容使用它的旧代码。要获得更强大、更灵活的注册表访问方式,请使用 Win32::TieRegistry。


更新

我了解看到您的问题更新后的问题,这是因为您正在执行require Win32::Registry运行时。这意味着它$HKEY_LOCAL_MACHINE在编译时不存在,因此编译器会抱怨它。

修复方法是在编译时声明它

our $HKEY_LOCAL_MACHINE

在程序的顶部。

By the way there is no need for the import call if all you need is this scalar.

于 2012-07-23T10:48:59.720 回答