Parse:...amples(3pm)

πŸ“¦
version 0.03 Β· 6 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸŽ€ Parse::DMIDecode::Examples(3pm)

okay bestie, welcome to the User Contributed Perl Documentation corner of the universe πŸ’– this one lives in section 3pm (that's "perl module" to the uninitiated, iykyk), and it's literally just a lil scrapbook of examples. no cap.


✨ NAME ✨

Parse::DMIDecode::Examples β€” examples using Parse::DMIDecode::Examples. yes, the page is describing itself with itself. main character energy fr fr, we love a self-referential icon πŸ’…


🌸 EXAMPLES 🌸

πŸ¦‹ Example 1: Find the Number of Physical CPUs

okay so this is the whole vibe: you spin up a Parse::DMIDecode object, you probe it, you walk every handle in the processor group, and you count the ones that are actually real physical CPUs and not some unpopulated ghost socket situation 😭

use strict;
use Parse::DMIDecode qw();

my $dmi = Parse::DMIDecode->new( nowarnings => 1 );
$dmi->probe;

my $physical_cpus = 0;
for my $handle ($dmi->get_handles(group => "processor")) {
    my $type = ($handle->keyword("processor-type") or "");
    next unless $type =~ /Central Processor/i;

    # Check the status of the cpu
    my $status = ($handle->keyword("processor-status") or "");
    if ($status !~ /Unpopulated/i) {
        $physical_cpus++;
    }
}

printf("There %s %d physical %s in this machine.\n",
        ($physical_cpus == 1 ? "is" : "are"),
        $physical_cpus,
        ($physical_cpus == 1 ? "CPU" : "CPUs"),
    );

a few lil notes on what's happening here, bestie 🫢

  • use strict; and use Parse::DMIDecode qw(); β€” pulling in the module with an empty import list, so nothing gets dumped into your namespace. respectful. iconic.
  • Parse::DMIDecode->new( nowarnings => 1 ) β€” constructing the object, and asking it to keep the warnings to itself.
  • $dmi->probe; β€” go get the DMI data. no probe, no data, no vibes.
  • $dmi->get_handles(group => "processor") β€” grabbing every handle in the processor group, then looping over them one by one.
  • $handle->keyword("processor-type") β€” pulling the processor-type keyword out of each handle. the or "" is there so a missing keyword becomes an empty string instead of a sad little undef πŸ’€
  • next unless $type =~ /Central Processor/i; β€” skipping anything that isn't a Central Processor (case-insensitive, we're not out here being picky).
  • $handle->keyword("processor-status") β€” same deal for the processor-status keyword, again with the or "" safety net.
  • if ($status !~ /Unpopulated/i) β€” the actual filter. if the status does not match Unpopulated, it's a real, populated CPU, so $physical_cpus++.
  • the printf β€” prints a sentence that grammatically adapts to the count. one CPU gets "There is 1 physical CPU", everything else gets "There are N physical CPUs". pluralisation ate, honestly πŸ”₯
the # Check the status of the cpu comment is part of the original code, so it stays exactly where it is. we do not touch the artifacts ✨

Copyright 2007 Nicola Worthington.

This software is licensed under The Apache Software License, Version 2.0.

<http://www.apache.org/licenses/LICENSE-2.0>


and that's the whole page, bestie 🌈 this was shipped with perl v5.36.0 and dated 2022-10-13, and it's filed under Parse:...amples(3pm). go count some CPUs, you absolute legend πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from