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'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|