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.
#!/usr/bin/perl 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 | |
by planetscape (Chancellor) on Jul 09, 2006 at 22:14 UTC | |
by swampyankee (Parson) on Jul 10, 2006 at 21:12 UTC | |
by !1 (Hermit) on Jul 10, 2006 at 02:46 UTC | |
by stonecolddevin (Parson) on Jul 10, 2006 at 20:59 UTC | |
by Anonymous Monk on Dec 07, 2007 at 16:08 UTC |
Back to
Cool Uses for Perl