#!/usr/local/bin/perl # mortgage_calculator.pl -- desc{ simple mortgage calculator CGI } # by Sean M. Burke, sburke@cpan.org # http://interglacial.com/~sburke/ # Time-stamp: "2005-08-21 15:54:27 ADT" use strict; #use CGI::Carp qw(fatalsToBrowser); use vars qw(%FORM $Debug); &main; exit; =head1 NAME mortgage_calculator.pl -- simple mortgage calculator CGI =head1 SYNOPSIS Call as a CGI. Modify the insides however you like. =head1 DESCRIPTION This is a simple program, meant to run as a CGI, that implements a loan calculator. The calculations are for loans amortized according to what I've been led to believe is the usual algorithm for loans in the USA. It seems to work just fine -- but if you use this calculator, or any of the data from it, you do so I. In other words: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. But let me know if it gives you any problems, OK? =head1 COPYRIGHT Copyright (c) 1999 Sean M. Burke. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR Sean M. Burke C =cut sub main { my $now; { my($m, $y) = (localtime($^T))[4,5]; ++$m; $y += 1900; $now = sprintf("%04d-%02d", $y, $m); } print "Content-type: text/html\n\n"; unless($ENV{'REQUEST_METHOD'} eq "POST") { &emit_table($now); exit; } %FORM = (); &get_form_data_POST; my $amount = abs(&canonize_number($FORM{"amount"}) || 100_000); # AKA "p" my $interest_rate = abs(&canonize_number($FORM{"interest_rate"}) || 8); my $term = abs(&canonize_number($FORM{"term"}) || 30); my $start_date = $FORM{"start_date"} =~ /^\d\d\d\d-\d\d$/s ? $FORM{"start_date"} : $now; $term = 99 if $term > 99; $interest_rate *= 100 if $interest_rate < 1; print <<"EOH"; Amortization Calculator

Amortization Table

EOH print "
\n";
  print &gen_table(
   { amount => $amount, term => $term, interest_rate => $interest_rate,
     start_date => $start_date
    });
  print "

Select here to Calculate with different parameters \n"; return; } sub canonize_number { my $in = $_[0]; $in =~ tr<-0-9.E+><>cd; return ($in + 0); } sub emit_table { my $time = $_[0]; my $selector = ''; foreach (0 .. 12) { $selector .= sprintf("