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

Hello there, I am using Win32::Registry in my program. It is recognized in the main program. But when I get into my package, let's say "myPackage " The Win32::Registry variables are undefined. I included "use Win32::Registry" inside the package but it doesn't help. Please help, Thanks a lot

Replies are listed 'Best First'.
Re: use inside a package
by johnnywang (Priest) on Dec 16, 2004 at 06:23 UTC
    The variables in Win32::Registry are exported to the main name space, so in your program you need to do something like:
    use strict; use Win32::Registry; package myPackage; print $::HKEY_LOCAL_MACHINE; # not $HKEY_LOCAL_MACHINE
    By the way, Win32::Registry is obsolete, the doc suggests one to use Win32::TieRegistry

    Updated: I went to look at the code in Win32::Registry.pm, and found the following segment, which should explain this issue pretty well.

    #define the basic registry objects to be exported. #these had to be hardwired unfortunately. # XXX Yuck! { package main; use vars qw( $HKEY_CLASSES_ROOT $HKEY_CURRENT_USER $HKEY_LOCAL_MACHINE $HKEY_USERS $HKEY_PERFORMANCE_DATA $HKEY_CURRENT_CONFIG $HKEY_DYN_DATA ); } $::HKEY_CLASSES_ROOT = _new(&HKEY_CLASSES_ROOT); $::HKEY_CURRENT_USER = _new(&HKEY_CURRENT_USER); $::HKEY_LOCAL_MACHINE = _new(&HKEY_LOCAL_MACHINE); $::HKEY_USERS = _new(&HKEY_USERS); $::HKEY_PERFORMANCE_DATA = _new(&HKEY_PERFORMANCE_DATA); $::HKEY_CURRENT_CONFIG = _new(&HKEY_CURRENT_CONFIG); $::HKEY_DYN_DATA = _new(&HKEY_DYN_DATA);
      Thanks man
Re: use inside a package
by Jenda (Abbot) on Dec 16, 2004 at 18:08 UTC

    As others already pointed out the variables were exported to main:: (GOK why, error in Win32::Registry). If you don't want to move to Win32::TieRegistry you my try my patch. If there ever is a new version of libwin32 it'll become the new official Win32::Registry. Or at least it should.

    Jenda
    We'd like to help you learn to help yourself
    Look around you, all you see are sympathetic eyes
    Stroll around the grounds until you feel at home
       -- P. Simon in Mrs. Robinson