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!
-
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 How to display code and escape characters
are good places to start.