Skip to content

Mutable state failure #5

@mrenvoize

Description

@mrenvoize
#!/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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions