Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Moose: using value of one attribute in another attribute

by mimosinnet (Beadle)
on Jan 11, 2013 at 23:16 UTC ( #1012994=perlquestion: print w/replies, xml ) Need Help??
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 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.

Replies are listed 'Best First'.
Re: Moose: using value of one attribute in another attribute
by tobyink (Abbot) on Jan 11, 2013 at 23:39 UTC

    You seem to be hard-coding information about a specific battery into the battery class.

    That would be like if I was trying to create a class to represent people, hard-coding my personal details as the defaults.

    use 5.010; { package Acpi::Info; use Moose; my @attrs = qw< name type online status present technology voltage_min_design voltage_now current_now charge_full_design charge_full charge_now model_name manufacturer serial_number >; for my $attr (@attrs) { has $attr => ( is => "ro", lazy => 1, default => sub { my $self = shift; $self->_build($attr) } ); } sub _build { my ($self, $key) = @_; my $data = $self->_data; $key = "POWER_SUPPLY_" . uc($key); $data =~ /^$key=(.+?)$/m and $1; } has file => ( is => "ro", isa => "Str", ); has _data => ( is => "ro", isa => "Str", lazy => 1, builder => '_build_data', ); sub _build_data { my $self = shift; local @ARGV = $self->file; local $/ = <ARGV>; } } # Usage # my $i1 = Acpi::Info->new(file => '/sys/class/power_supply/BAT1/uevent' +); my $i2 = Acpi::Info->new(file => '/sys/class/power_supply/ACAD/uevent' +); for my $supply ($i1, $i2) { say "----"; for my $attr ($supply->meta->get_all_attributes) { my $aname = $attr->name; next if $aname =~ /^_/; say $aname, " = ", $supply->$aname; } }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks very much for you clear and detailed answer! A good insight on how objects work! It is also very enlightening to understand your example on how to read the contents of the file. Also, I was not aware of the use of the underscore to make attributes and builders private!

        Perl doesn't have true private methods. Underscores are merely a convention to indicate that a sub is intended for internal use only.

        It is possible to create do-it-yourself private subs by checking caller within a sub and then calling die if the caller is outside your module. Though this will have a performance impact if you do it a lot, and is of questionable benefit.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012994]
Front-paged by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-04-21 10:19 GMT
Find Nodes?
    Voting Booth?