Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following handles basic maths. It should be fairly obvious how to extend it to cover other mathematical operations.

lib/Scalar/Annotated.pm

package Scalar::Annotated; use 5.010; use strict; use utf8; use Carp; use Scalar::Util qw/looks_like_number blessed/; sub _swap { my ($sub, $x, $y, $swap) = @_; $swap ? $sub->($y, $x) : $sub->($x, $y); } sub _promote { map { blessed($_) && $_->isa(__PACKAGE__) ? $_ : an($_) } @_ } use namespace::clean; use parent qw/Exporter/; use Object::AUTHORITY; use Object::DOES; use Object::Stash -type => 'object'; our ($AUTHORITY, $VERSION, @EXPORT); BEGIN { $AUTHORITY = 'cpan:TOBYINK'; $VERSION = '0.001'; @EXPORT = qw/an/; } sub new { my ($class, $value, $derivation) = @_; croak "Needs to be a simple scalar" if ref $value; $derivation //= looks_like_number($value) ? $value : "q{$value}"; my $self = bless \$value, $class; $self->derivation = $derivation; return $self; } sub an { return __PACKAGE__->new(@_); } sub derivation :lvalue { my $self = shift; $self->stash->derivation(@_) } use overload '+0' => sub { ${ $_[0] } }, q{""} => sub { ${ $_[0] } }, '+' => sub { _swap(\&add, @_) }, '-' => sub { _swap(\&subtract, @_) }, '*' => sub { _swap(\&multiply, @_) }, '/' => sub { _swap(\&divide, @_) }, '%' => sub { _swap(\&modulus, @_) }, ; sub add { my ($x, $y) = _promote(@_); return an( $$x + $$y, sprintf('(%s + %s)', $x->derivation, $y->derivation), ); } sub subtract { my ($x, $y) = _promote(@_); return an( $$x - $$y, sprintf('(%s - %s)', $x->derivation, $y->derivation), ); } sub multiply { my ($x, $y) = _promote(@_); return an( $$x * $$y, sprintf('(%s × %s)', $x->derivation, $y->derivation), ); } sub divide { my ($x, $y) = _promote(@_); return an( $$x / $$y, sprintf('(%s ÷ %s)', $x->derivation, $y->derivation), ); } sub modulus { my ($x, $y) = _promote(@_); return an( $$x % $$y, sprintf('(%s mod %s)', $x->derivation, $y->derivation), ); } __PACKAGE__

example.pl

use 5.010; use strict; use utf8::all; use lib "lib"; use Scalar::Annotated; my $foo = an(5, '$foo'); my $bar = an(2, '$bar'); my $baz = 2 * ($foo + $bar - 1); my $quux = $baz % $foo; say '$quux is ', $quux; say '$quux was calculated as ', $quux->derivation; # Reset the derivation of $quux, because we are no longer # interested in how it was derived. $quux->derivation = '$quux'; $quux *= 2; say '$quux is now ', $quux; say '$quux was calculated as ', $quux->derivation;

Output

$quux is 2 $quux was calculated as ((2 × (($foo + $bar) - 1)) mod $foo) $quux is now 4 $quux was calculated as ($quux × 2)

In reply to Re: annotation, derivation by tobyink
in thread annotation, derivation by hv

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found