blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Dear All
I need a little help please,
I have just started on writing a script that interrogates the SAM database from the Registry. I started with the following code and got this error message.


"my" variable $main::HKEY_LOCAL_MACHINE can't be in a package at
C:\Perl\reg.pl line 9,
near "my $main::HKEY_LOCAL_MACHINE"
Execution of C:\Perl\reg.pl aborted due to compilation errors.

I cannot progress any further unless this first step is working.Your help is highly appreciated
use strict VAR; use warnings 'all'; use Win32API::Registry; use Win32API::Net; use Win32::TieRegistry; use Win32; my $CurrVer; my $p = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"; my $main::HKEY_LOCAL_MACHINE->Open($p, $CurrVer) || die "OPEN $!\n";

Replies are listed 'Best First'.
Re: Extracting Registry information
by Ido (Hermit) on Jun 19, 2002 at 10:58 UTC
    From Programming Perl(The Camel), 3rd Edition, Page 56, Second paragraph: Variables attached to a lexical scope are not in any package, so lexically scoped variable names may not contains the :: sequence. (Lexically scoped variables are declared with a my declaration.)
    Remove the main:: and make it $HKEY_LOCAL_MACHINE.
      Many Thanks Brother, with today’s two replays I recieved for my queries I sincerely began to see the light at the end of the tunnel again.
        It seems that my first post here has been deleted?
(tye)Re: Extracting Registry information
by tye (Sage) on Jun 19, 2002 at 15:59 UTC

    You appear to be confusing Win32::Registry usage with Win32::TieRegistry. Win32::TieRegistry doesn't define a HKEY_LOCAL_MACHINE variable.

    Because this type of stuff is easy to confuse, I strongly encourage making it explicit what you are importing from each module:

    use strict 'vars'; use warnings 'all'; my $Registry; use Win32::TieRegistry( TiedRef => \$Registry ); my $CurrVer= $Registry->{"HKEY_LOCAL_MACHINE\\" . "Software\\Microsoft\\Windows NT\\CurrentVersion\\" ) or die "Can't open SW/MS/WinNT/CurrentVersion: $^E\n";

            - tye (but my friends call me "Tye")