http://qs321.pair.com?node_id=30631


in reply to RE: RE: RE: Win32::MemoryInfo
in thread Win32::MemoryInfo

Here's what I came up with after some reworking. I decided to go with a hash reference as the main parameter instead of taking words. This also means that it must now be called like this:
my %hMem = (TotalPhys =>0, AvailPhys =>0); Win32::MemoryInfo::MemoryStatus(%hMem,"MB");

I don't know what the consensus is on putting values into a hash like that, but if someone doesn't check the return value at least they won't get bit with undef values.
Here's the reworked function:
sub MemoryStatus (\%;$) { my $return = shift; my $ret_type = shift; my %fmt_types = ( B => 1, KB => 1024, MB => 1024*1024, GB => 1024*1024*1024); my @params = qw(MemLoad TotalPhys AvailPhys TotalPage AvailPage TotalVirtual AvailVirtual); my %results; my $MemFormat; my $dwMSLength; $MemFormat = ($ret_type =~ /^[BKMG]B?$/) ? $fmt_types{$ret_type} : $fmt_types{B} +; my $GlobalMemoryStatus ||= new Win32::API("kernel32", "GlobalMemoryStatus", ["P"], "V") or ret +urn undef; my $MEMORYSTATUS = pack "L8",(0, 0, 0, 0, 0, 0, 0, 0); $GlobalMemoryStatus->Call($MEMORYSTATUS); ($dwMSLength, @results{@params}) = unpack "L8", $MEMORYSTATUS; return undef if ($dwMSLength == 0); if (keys(%$return) == 0) { foreach (@params) { $return->{$_} = ($_ eq "MemLoad") ? $results{$_} : $results{$_}/$M +emFormat; } } else { foreach (@params){ $return->{$_} = $results{$_}/$MemFormat unless (!defined($return- +>{$_})); } } 1; }


Guildenstern
Negaterd character class uber alles!