Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

hilitai, with the ideas generated by the other Monks, I was able to come up with this, which worked for me on Win7 strawberry 5.24.1 and linux perl 5.8.5.

#!/usr/bin/perl use strict; use warnings; my $usemod; # if you initialize `my $usemod = 0;`, then the BEGIN +block's assignment to $usemod is overridden, which you don't want our $Registry; # this will be overridden by Win32::TieRegistry's expo +rt of $Registry if in Windows, but BEGIN { local $\ = "\n"; if( $^O eq 'MSWin32' ) { eval { require Win32::TieRegistry; Win32::TieRegistry->import( Delimiter => '/' ); $usemod = 1; } } } if ($usemod) { my $IEKey = $Registry->{'HKEY_CURRENT_USER/Software/Microsoft/Inte +rnet Explorer/Main/'}; print "IEKey = '$IEKey'\n"; } else { print "None of that stuff here\n"; }
  • my 5.8.5 linux perl didn't like the use if ... (edit: or, at least, the way I tried... with some debug, it may have eventually worked), so I went with Discipulus's BEGIN block idea to make that run first

  • removing the = 0 on my $usemod; allowed the $usemod variable to update in the BEGIN block
  • used Perlbotics' suggestion of ->import(), though I included your Delimiter => '/' argument to the import
  • made $Registry a package variable using our, which then allowed the Win32::TieRegistry->import() to clobber it with the defined version from the module, while still allowing linux perl to compile the script

edit#2: added use warnings; per best practices

edit#3: added "perl" to "linux perl 5.8.5", so it wasn't implying that was the version of linux, but rather the version of perl


In reply to Re: Conditional use of Win32::TieRegistry by pryrt
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-05-14 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?