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;
}
GuildensternNegaterd character class uber alles!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.