I have a script that is used on Windows and Linux machines. Now, I need to have the script make a registry change on windows. Using Win32::TieRegistry seems reasonable, however I can't figure out a way to have this module work on Windows, and at the same time not get @INC errors on Linux.

My problem, I think, is that Win32::TieRegistry module appears to create a globally-scoped Registry object when the interpreter hits the 'use ...' statement. This object is used in all further interaction with the registry. If I try to put the 'use Win32::TieRegistry' into an 'eval {}', as I do when trying to load modules that may not exist, I get "Global symbol $Registry requires explicit package name..." errors.

Below is a simple script that encapsulates my problem. Two lines are commented out; if either one is uncommented, the script will work on either Windows *or* Linux; but if both are either left alone or uncommented, it fails on both platforms:

#!/usr/bin/perl use strict; my $usemod = 0; eval { require Win32::TieRegistry; }; if ($@) { warn "No module\n"; } else { $usemod = 1; } if ($usemod) { # my ($Registry, $IEKey); # If I expose this line, Windo +ws FAILS ($IEKey remains null); comment out, Windows WORKS # use Win32::TieRegistry(Delimiter=>"/"); # If I expose this line, L +inux FAILS (@INC compile error); comment out, Linux WORKS my $IEKey = $Registry->{'HKEY_CURRENT_USER/Software/Microsoft/Inter +net Explorer/Main/'}; print $IEKey, "\n"; } else { print "None of that stuff here\n"; }

Enlightenment sought, thanks in advance.


In reply to Conditional use of Win32::TieRegistry by hilitai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.