Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to Moose: using value of one attribute in another attribute by mimosinnet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2014-11-28 07:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (193 votes), past polls