http://www.perlmonks.org?node_id=1012994
mimosinnet has asked for the wisdom of the Perl Monks concerning the following question:

The Acpi::Battery distribution uses the old location for battery information (/proc/acpi/battery/...) and, as an exercise (still in the basic learning process), I have tried to see if I could replicate the code with the new location (/sys/class/power_supply/...). In the process, this code seems to easily get the values from the file /sys/class/power_supply/uevent:

The package Acpi::Battery::Values gets the values from the file /sys/class/power_supply/uevent. Very unsure if this is the right approach, although somehow it works!.

package Acpi::Battery::Values; use Acpi::Arxiu; # Reads the content of the file use Moose; has 'directory' => ( is => "ro", isa => "Str", ); my $dir = "/sys/class/power_supply/BAT1/uevent"; my $object = Acpi::Arxiu->new(filename => "$dir"); my $content = $object->content; my %value = $content =~ /^POWER_SUPPLY_(\w+)=(.+)$/mg; foreach (keys %value) { has lc($_) => ( is => "ro", default => $value{$_}, ); } 1;

This is the code that calls to Acpi::Battery::Values

use FindBin qw($Bin); use lib "$Bin/lib"; use strict; use warnings; use utf8; use Acpi::Battery::Values; use Moose; my $a = Acpi::Battery::Values->new(directory => "/sys/class/power_supp +ly/BAT1/uevent"); my $b = $a->model_name; print "$b \n"; my $meta = Class::MOP::Class->initialize("Acpi::Battery::Values"); for my $attr ( $meta->get_all_attributes ) { my $an = $attr->name; my $value = $a->$an; print "$an = $value \n"; }

Running the code produces this output;

perl Values.pl G71C000AH310 serial_number = 0000000288 power_now = 0 status = Unknown directory = /sys/class/power_supply/BAT1/uevent model_name = G71C000AH310 cycle_count = 0 energy_full_design = 54540000 name = BAT1 energy_full = 43038000 energy_now = 45269000 capacity = 105 voltage_min_design = 10800000 technology = Li-ion present = 1 voltage_now = 11360000

I am stuck at how to use the value of the attribute 'directory' in the Acpi::Battery::Values object to dynamically create the other attributes, so different 'batteries' can be read. I very much appreciate any hints on code or documentation.

Thanks for your attention.