http://www.perlmonks.org?node_id=1026553

Some time ago I wanted to get the battery information in my linux box. I knew of sys-power/acpi, and I checked if there was anything I could use in Perl. There is the Acpi::Battery module, but this module gets information from the deprecated directory /proc/acpi. Improving the module looked like an affordable task for a beginner and I ended up writing a general module, Acpi::Class, that could read information from the directory /sys/class.

Acpi::Class has been my first module, and I have recently been able to emulate the sys-power/acpi after learning to "read" some C. This is the code:

#!/usr/bin/env perl # {{{ Description #===================================================================== +========== # Print Battery Information using Acpi::Class # Emulate acpiclient: http://sourceforge.net/projects/acpiclient/ #===================================================================== +========== # }}} # {{{ Modules use strict; use warnings; use Acpi::Class; # }}} # {{{ Define Variables # We will get information of BAT1: /sys/class/power_supply/BAT1 my $my_class = "power_supply"; my $my_device = "BAT1"; my $class = Acpi::Class->new( class => $my_class, device => $my_device +); my $values = $class->g_values; my @attributes = qw(energy_now charge_now power_now current_now energy_full charge_full energy_full_design charge_full_design status capacity); # }}} # {{{ Obtain and calculate values my %att_value; foreach my $attribute (@attributes) { $att_value{$attribute} = $values->{$attribute}; } my $remaining_capacity = $att_value{'energy_now'} // $att_value{'charg +e_now'}; my $last_capacity = $att_value{'energy_full'} // $att_value{'char +ge_full'}; my $present_rate = $att_value{'power_now'} // $att_value{'curren +t_now'}; my $design_capacity = $att_value{'energy_full_design'} // $att_valu +e{'charge_full_design'}; my $percentage = $att_value{'capacity'} // $remaining_capacity + * 100 / $last_capacity; my $battery_loss = 100 - $last_capacity * 100 / $design_capacity +; my $status = $att_value{'status'}; my $seconds = 0; my $message = ""; if ( $status =~ /Discharging/ ) { $seconds = 3600 * $remaining_capacity / $present_rate; $message = "Remaining"; } elsif ( $status =~ /Charging/ ) { $seconds = 3600 * ($last_capacity - $remaining_capacity) / $presen +t_rate; $message = "Until Charged"; if ($remaining_capacity > $last_capacity) { $seconds = 3600 * $remaining_capacity / $present_rate; $message = "Remaining, Charging on top last recorded full capa +city"; } } elsif ( $status =~ /Unknown/ ) { $status = "Full"; } else { die "status = $att_value{'status'} and current_now = $present_rate +" } my $there_are_seconds = $seconds; my $there_is_battery_loss = $battery_loss; my $minutes = int($seconds / 60); $seconds = $seconds % 60; my $hours = int($minutes / 60); $minutes = $minutes % 60; # }}} # {{{ Print values my $time = ""; $time = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds) +if ($there_are_seconds); $percentage = sprintf("%.0f%%", $percentage); $battery_loss = sprintf("%.1f%%", $battery_loss); print "$my_device: $status $percentage, "; print "$time $message, " if ($there_are_seconds); print "$battery_loss Battery Loss \n" if ($battery_loss); # }}}

I would very much appreciate some indications on which should be the right direction to follow. Here some options I have thought about:

  1. Improve the Acpi::Class module. Much more needs to be done.
  2. Improve the submitted code. Much more needs to be done.
  3. Transform this code into a module, so other people can use the module to get battery information and see how they can use Acpi::Class to develop code to get information on other devices.
  4. Improve the documentation. It is very difficult to understand what this is all about
  5. More time should be spend in more important Perl modules.
  6. There are other areas, like learning English or gardening, that could make people happier than seeing this code.
  7. All of the above

Actually, I am considering the three first points ;-) . I would very much appreciate if you have some suggestions on how to improve the code of if you think this should be transformed into a module.

Thanks and happy Perling!

P.S.: I have been able to grasp "some basic" C after learning "some" Perl. I have just realised how Perl makes life easier in understanding other languages and producing some code ;-).