App-InvestSim
view release on metacpan or search on metacpan
lib/App/InvestSim/Values.pm view on Meta::CPAN
# This package exposes two variables %values and %values_config that hold all
# the configuration variables of the simulation and some description of these
# variables.
# The package also exposes a set of function to read and write %values to and
# from files.
package App::InvestSim::Values;
use 5.022;
use strict;
use warnings;
use App::InvestSim::Config ':array';
use App::InvestSim::LiteGUI ':all';
use Exporter 'import';
use File::HomeDir;
use File::Spec::Functions 'catfile';
use Hash::Util;
use List::Util qw(pairmap);
use Data::Dumper;
use Safe;
our @EXPORT = ();
our @EXPORT_OK = qw(%values %values_config $has_changes init_values save_values save_values_as open_values autoload autosave);
our %EXPORT_TAGS = (all => \@EXPORT_OK);
# Validation functions for different value type. Each of these functions receive
# two arguments:
# - the string to validate.
# - the validation event: 'key' when the validation is for a key entered by the
# user, 'focusout' when the value is complete and it can be validated
# entirely. That second event is also used to validate values read from a
# backup file.
# On failure, these methods must all return 0 (and not just '' which is the
# default false value), as that is expected by Tcl.
# An integer that is allowed to be 0 or more.
sub validate_non_negative_integer {
my ($val, undef) = @_;
if ($val =~ m/^\d*$/) {
return 1;
} else {
return 0; # Tcl requires a 0 and not just ''.
}
}
# An integer that must be positive.
sub validate_positive_integer {
my ($val, $event) = @_;
if ($val =~ m/^\d*$/ and ($val or $event eq 'key')) {
return 1;
} else {
return 0; # Tcl requires a 0 and not just ''.
}
}
# A non-negative float.
sub validate_float {
my ($val, $event) = @_;
if ($val =~ m/^\d*((\.|,)\d*)?$/) {
return 1;
} else {
return 0;
}
}
# The file name used for the auto-save feature.
my $autosave_file = catfile(File::HomeDir->my_data(), '.investment_simulator');
# The default options used for the getSaveFile and getOpenFile dialog boxes.
my @open_save_options = (
-defaultextension => '.investment',
-filetypes => [["Simulation d'investissement", [".investment"]],
["Tout les fichiers", ["*"]]]);
my $current_file;
# This hash list all the variables that can be used to configure the simulation.
# Each variable name points to the following values (in an array-ref):
# - its default value (which also give its type if its a reference);
# - a validation function for values entered by the user or read from files;
# - for an array variable, its expected size.
our %values_config = (
# Values set through the left-bar of the program:
invested => [300000, \&validate_positive_integer],
tax_rate => [41.0, \&validate_float],
base_rent => [800, \&validate_positive_integer],
rent_charges => [24, \&validate_float],
rent_increase => [0.5, \&validate_float],
duration => [20, \&validate_positive_integer],
loan_insurance => [0.3, \&validate_float],
other_rate => [0.0, \&validate_float],
social_tax => [17.2, \&validate_float],
( run in 1.922 second using v1.01-cache-2.11-cpan-524268b4103 )