#!/usr/bin/perl -w # # mort - tchrist@perl.com use strict; use vars qw ( $short $loan $down $years $extra $xmas $rate $months $owed $monthly $per_month $date $this_month $year $month $payment $paid $interest $td_int $td_pay $to_prince $smon $syear @monames $from_cgi ); @monames = qw{ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec }; $loan = $down = $years = $extra = $xmas = $rate = $months = $owed = $monthly = $per_month = $year = $month = $payment = $paid = $interest = $td_int = $td_pay = $to_prince = 0.00; $loan = 200000; $down = "0%"; $years = 30; $extra = 0; $xmas = 0; $short = 0; $rate = 8.75; $from_cgi = 0; ($smon, $syear) = (localtime(time()))[4,5]; $smon += 2; for (@ARGV) { my ($var, $val); no strict 'refs'; if ( ($var, $val) = split /\s*=+\s*/ ) { unless (defined $$var) { die <>> @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>>>>>>> @>>>>>>>>>> @>>>>>>>>>> { $date, $month, commify($payment), commify($interest), commify($to_prince), commify($td_pay), commify($td_int), commify($owed), commify($paid) } . $^L = ''; $date = 'Initial
Loan
' if $from_cgi; if ($from_cgi) { printf "

Mortgage Calculation Results

"; print "
"; table_header(); table_write(); } else { write; } sub table_header { print "

"; print ""; for ( 'Month', 'Pay #', 'Pay Now', 'Interest', 'Principal', 'YTD Paid', 'YTD Intrst', 'Still Owed', 'Total Paid' ) { print "\n"; } print ""; } for ( $month = 1; $owed > 0; $month++ ) { $payment = $per_month; $this_month = -1 + ($smon + $month) % 12; $year = $syear + int (($smon + $month - 2) / 12); if ($extra =~ m/\$(.+)/) { $payment += $1; } else { $payment += ($per_month * $extra/12); } if ($xmas && $this_month % 12 == 0) { if ($xmas =~ m/\$(.+)/) { $payment += $1; } else { $payment += $per_month * $xmas; } } $payment = sprintf("%.2f", $payment); # can only pay in integer pennies $interest = $owed * $monthly; $td_int += $interest; $td_pay += $payment; if ($interest + $owed < $payment) { $payment = $owed + $interest; } $to_prince = $payment - $interest; $owed -= $to_prince; $paid += $payment; $date = sprintf "%3s %04d", $monames[$this_month-1], $year; if (!$short || $this_month % 12 == 0 || $owed < 0.01) { if ($owed < 0.01) { $date .= "
Final Balance" if $from_cgi; } unless ($from_cgi) { write; } else { table_write(); if ($this_month % 12 == 0 && !$short) { table_footer(); table_header(); } } } if ($this_month % 12 == 0) { $td_pay = $td_int = 0; $- = 0 unless $short; } # print "Backsliding! Paying $to_prince to principal" if $to_prince < 0; } if ($from_cgi) { table_footer(); print "
"; print "

Congratulations!

You just paid \$", commify($paid - $loan), " in (deductable) interest for that \$", commify($loan), " loan!"; } sub table_footer { print ""; print "
$_
Payments through $year
"; } sub commify { local $_ = sprintf "%.2f", shift; 1 while s/^(-?\d+)(\d{3})/$1,$2/; return $_; } sub table_write { print ""; print "$date"; print "$month"; for ( $payment, $interest, $to_prince, $td_pay, $td_int, $owed, $paid ) { print "", commify($_), ""; } print "\n"; }