Description: This is a simple piece of Perl I wrote to see what percentage my laptop battery was. The die needs to be more descriptive and I also need to take out the hardcoded values for a full battery, but it gets the job done faster than cat and scanning.
use warnings;
use strict;
use diagnostics;
my $status;
open (BATT, "/proc/acpi/battery/BAT0/state") || die("Can't Open: $!");
while (<BATT>) {
  if ($_ =~ /(charging state:)(\s+)(\w+)/ ) {
    $status = $3;
  if ($_ =~ /(:)(\s+)(\d*)(\s+)(mAh)/ ) {
     print "\n";
     my $perc = $3/2791;
     printf "The battery is %s", $status;
     print "\n";
     printf "Battery percentage is: %.0f", $perc*100;
     print "\n-----------------------------\n\n";
     } else {
     print "Incorrect Format";
close BATT;
Replies are listed 'Best First'.
Re: Checking Laptop battery info
by stonecolddevin (Parson) on Jul 09, 2006 at 09:17 UTC
    what would it take for something like this to be done for windows/mac?

        Micro$oft does have VBS code, which can serve as a starting point. After all, translating from VBS to Perl should be easy &foolish_grin;

        Microsoft scripting center


        e(π√−1) = −1

      For win you could do something like this:

      #!/usr/bin/perl -l use Win32::OLE('in'); my $objWMIService = Win32::OLE->GetObject("winmgmts:\\\\.\\root\\cimv2 +") or die "WMI connection failed.\n"; $colItems = $objWMIService->ExecQuery("Select * from Win32_Battery","W +QL",0x30); print "$_->{DeviceID} ($_->{Description}): $_->{EstimatedChargeRemaini +ng}%" for in $colItems;

      Of course, I get percentages greater than 100% when I'm plugged in so it isn't completely ideal. =/

        well it's most certainly a start. if nothing else i can be like, "hey dad, check this out" :-)

        might even be able to play around with it and see what the problem is with the percentages...