in reply to
Moose: using value of one attribute in another attribute
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'