-
Notifications
You must be signed in to change notification settings - Fork 3
Mutable state failure #5
Copy link
Copy link
Open
Description
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Money;
my $debt = 90.00; # Debts are stored as positives in the database
my $debt_money = Data::Money->new(value => $debt, code => 'GBP');
my $subtraction = $debt_money;
print "Debt: " . $debt . "\n";
print "Debt Money: " . $debt_money . "\n\n";
print "Doing money sum $debt_money -= $subtraction\n";
$debt_money -= $subtraction;
print "Money amount after:" . $debt_money . "\n";The above script highlights a mutable state issue; In the above snippet we can see in this case that the in place subtraction £90 --= $90 should result in £0 but actually yields -£90.
I asked on #perl-help and it appears to stem from $amount_to_subtract containing the same object as $debt_money.. which is the case when $credit_money >= $debt_money.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels