Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #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.


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__

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;


$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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Corion]: erix: There is somebody who has hacked up some Ruby scripts to do just that, but the Ruby scripts have some hardcoded #defines (well, reimplement those), so the extraction doesn't compile for Windows...
    [Corion]: libpg_query
    [Corion]: ... and I think "porting" it basically means (re)implementing a C preprocessor and feeding that one with the appropriate #defines for the platforms, and/or providing stub implementations for OS-specific functions that the query parser really wants to call
    [erix]: ah, interesting. Well, sounds like a good start..
    [Corion]: In the long run, obviously, the parser wouldn't need/call OS-specific stuff anyway, but we're a long way from that :D

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (7)
    As of 2018-07-23 11:32 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (464 votes). Check out past polls.