Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

how do I use Win32::TieRegistry in a multi-platform program?

by perltux (Monk)
on Mar 01, 2018 at 20:17 UTC ( [id://1210183] : perlquestion . print w/replies, xml ) Need Help??

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

If I use Win32::TieRegistry in a program meant to run both on Windows an Linux I get the following error when running it on Linux:

Global symbol "$Registry" requires explicit package name at ./script.pl line 16.
Execution of ./script.pl aborted due to compilation errors.

The code that leads to this is (this is only an extract of a much larger program):
#!/usr/bin/perl use strict; use warnings; my $LINUX; my $WINDOWS; BEGIN{ if ($^O eq 'linux') { $LINUX=1; } elsif ($^O eq 'MSWin32') { $WINDOWS=1; } } use if ($WINDOWS), 'Win32::TieRegistry' => (Delimiter => '/'); if ($WINDOWS) { my $mykey=$Registry->{'HKEY_CURRENT_USER/Control Panel/Desktop/Win +dowMetrics'}; my $wdpi=hex($mykey->{'AppliedDPI'}) .'dpi / '; }


The problem is that $Registry is undefined when this program runs on Linux. How do I solve this?
Many thanks in advance

Replies are listed 'Best First'.
Re: how do I use Win32::TieRegistry in a multi-platform program?
by haukex (Archbishop) on Mar 01, 2018 at 20:47 UTC
      many thanks, I can confirm too that your solution works! I don't know how I missed the other thread, I did search for this (with google) before posting here.
Re: how do I use Win32::TieRegistry in a multi-platform program?
by ikegami (Patriarch) on Mar 04, 2018 at 02:02 UTC

    I'd create a module for each platform, and load the appropriate module.

    sub get_sys_info { if ($^O eq 'Win32') { require My::SystemInfo::Win32; return My::SystemInfo::Win32->new(); } else { require My::SystemInfo::Unix; return My::SystemInfo::Unix->new(); } } my $sys_info = get_sys_info(); ... $sys_info->wdpi ...
A reply falls below the community's threshold of quality. You may see it by logging in.