NAME
Graphics::Grid::UnitArithmetic - Expression created from Graphics::Grid::Unit objects
VERSION
version 0.0000_03
SYNOPSIS
use Graphics::Grid::UnitArithmetic;
use Graphics::Grid::Unit;
my $ua1 = Graphics::Grid::UnitArithmetic->new(
node => '+',
children => [
Graphics::Grid::Unit->new( [ 1, 2, 3 ], "cm" ),
Graphics::Grid::Unit->new(0.5),
],
);
my $ua2 = Graphics::Grid::UnitArithmetic->new(
Graphics::Grid::Unit->new(0.1, "npc")
);
my $ua3 = $ua1 - $ua2;
my $ua4 = $ua1 * 2;
# or use the function interface
use Graphics::Grid::Functions qw(:all);
my $ua = unit(@params) * 2;
DESCRIPTION
You would mostly never directly use this class. See Graphics::Grid::Unit for unit arithmetic on unit objects.
This class Graphics::Grid::UnitArithmetic represents arithmetic on Graphics::Grid::Unit objects. It provides a way to create a unit-like value that combines both relative and absolute values.
Supported operators are +
, -
, and *
. A plus or minus operation requires both its binary operands are consumers of Graphics::Grid::UnitLike. The multiply operation requires one of its operands is consumer of Graphics::Grid::UnitLike, the other a number or array ref of numbers.
ATTRIBUTES
node
It can be one of the followings,
A number or an array ref of numbers. If a single number is given it will be coerced to an array ref that contains the number. This is used for operands of multiplications.
One of
"+"
,"-"
,"*"
. In this case thechildren
attr should contain the operands.A Graphics::Grid::Unit object. This is used for operands of plus and minus.
children
When the object represents an arithmetic operation, this children
attribute has the operands.
METHODS
at($idx)
This method returns an object of the same Graphics::Grid::UnitArithmetic class. The returned object represents the data at given index, and has at only one value at each node.
# $ua1 has 3 elems: 1cm+0.5npc, 2cm+0.5npc, 3cm+0.5npc
my $ua1 = Graphics::Grid::UnitArithmetic->new(
node => '+',
children => [
Graphics::Grid::Unit->new( [ 1, 2, 3 ], "cm" ),
Graphics::Grid::Unit->new(0.5),
],
);
# $ua2 has only 2cm+0.5npc
my $ua2 = $u1->at(1);
$idx
is applied like wrap-indexing. So below is same as above.
my $ua3 = $ua1->at(4);
elems
Return the number of effective elements in the object.
is_unit
Checks if the object is a Graphics::Grid::Unit.
is_number
Checks if the object is an array ref of numbers.
is_arithmetic
Check is the object is an arithmetic operation. It is equivalent to !($obj-≥is_unit() or $obj-≥is_number())
.
stringify
Stringify the object.
SEE ALSO
AUTHOR
Stephan Loyd <sloyd@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2018 by Stephan Loyd.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.