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

I have a problem with the KEY_READ Win32::Registry function in a package/class object I'm creating.

The specific code is:

my $regKey = $Registry->Connect( $self->{ _csii_server_name }, get_registry_path . "\\SQL" ,{ Access => $Registry->KEY_READ } )

The error I get is:

Can't call method "KEY_READ" on an undefined value at C:/Perl/site/lib/cmpny/cmpny_SQLSERVER_INSTANCE_INFORMATION.pm line 75.

The rest of the code for the module etc is in my public scratchpad OzVegan's Public Scratchpad I'm having a hard time trying to pin down why KEY_READ is not being recognised even though it works no problems in a standard Perl script. I'm missing some logic or something :-/

Replies are listed 'Best First'.
Re: Can't call method "KEY_READ" on an undefined value
by wind (Priest) on May 11, 2011 at 00:04 UTC

      What's the difference between use and package? I thought package was to name the actual package not import one. I could be mistaken on its uses. It works with use no issues in other packages though.

        Okay, so during debugging I tested $Registry only to find it was undefined at the point I needed it. I tried wind's suggestion and set $Registry to our $Registry and hey presto it is no longer undefined.

        IT WORKS! So now I have to look into the our keyword a bit more. There's something I've missed. Thanks for the help.

Re: Can't call method "KEY_READ" on an undefined value
by ikegami (Patriarch) on May 11, 2011 at 00:01 UTC
    Did you forget to import it? (I would expect a strict error if that was the case, but maybe you're foolishly not using use strict;.)

      I tried importing it with the following:

      require Win32::TieRegistry; import Win32::TieRegistry;

      Still no joy. I even tried putting it in the @ISA array out of desperation.

Re: Can't call method "KEY_READ" on an undefined value
by locked_user sundialsvc4 (Abbot) on May 11, 2011 at 01:46 UTC

    our gives access to a variable having package-wide scope.   Well, sort of ...   From perldoc -f our:

    “our” associates a simple name with a package variable in the current package for use within the current scope.   When “use strict ‘vars’” is in effect, “our” lets you use declared global variables without qualifying them with package names, within the lexical scope of the “our” declaration.   In this way "our differs from “use vars,” which is package scoped.

    Unlike “my,” which both allocates storage for a variable and associates a simple name with that storage for use within the current scope, “our” associates a simple name with a package variable in the current package, for use within the current scope.   In other words, “our” has the same scoping rules as “my,” but does not necessarily create a variable.

      Thank you for this explanation. I read a few articles on it and PerlDoc but neither clearly explained the package scope and I thought I may be creating a global variable for all packages..