Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w # Please run with -h for help # Please run with -c for configuration (first time) use strict; # use Date::Calc; -- for later my ($i, %conf); CheckArg(@ARGV) if @ARGV; die "file simplemoney.conf does not exist, please run -c to configure\ +n" unless (-e "simplemoney.conf"); #check for existance of configurat +ion file open (RCONF, "simplemoney.conf") || die "Could not open simplemoney.c +onf $!"; foreach ("rt", "balance", "transaction"){ #load config variables into +a hash chomp ($conf{$_} = <RCONF>); } close (RCONF) || die "Could not close simplemoney.conf $!"; unless (-e $conf{rt} && -e $conf{balance} && -e $conf{transaction}) { + die "One of the required files is missing, please run with -c to co +nfigure\n"; } while (1) { my ($command, $arg1, $arg2); print "Your command (help for list of commands):\n"; chomp ($command = <STDIN>); if ($command =~ /^balance/) { print CalcBalance($conf{balance}); } elsif ($command =~ /^help/i) { PrintHelp(); } elsif ($command =~ /^deposit/i) { (undef, $arg1, $arg2) = split (/\s+/, $command); CalcBalance($conf{balance},$arg1); AddTransaction($conf{transaction},$arg2); } elsif ($command =~ /^withdraw/i) { (undef, $arg1, $arg2) = split (/\s+/, $command); $arg1 = -$arg1; CalcBalance($conf{balance},$arg1); AddTransaction($conf{transaction},$arg2); } elsif ($command =~ /^rt/i) { print "RT is not yet supported."; } elsif ($command =~ /^exit/i || $command =~ /^quit/i || $command +=~ /^e/i || $command =~ /^q/i) { exit 0; } else { #must be undefined command, try again. print "\nBad Command\n"; next; } } ###Subs### sub PrintHelp { print "This program calculates your money balance, please run with the + -c flag to configure.\n"; print "Flags:\n"; print " -c -- Configure\n"; print " -h -- This file\n"; print "Default Files:\n"; print " rt.log -- Reocuring transaction configuration\n"; print " balance.log -- Backlog of user's balance\n"; print " transaction.log -- Backlog of transaction descript +ions\n"; print " Configuration File: simplemoney.conf\n"; print "Commands:\n"; print " help -- show this file.\n"; print " deposit <arg1> <arg2> -- deposit <arg1> dollars with +<arg2> description in the log.\n"; print " withdraw <arg1> <arg2> -- same as above, but withdraw +.\n"; print " rt -- not yet supported.\n"; print " exit, quit, e, or q -- quit the program.\n"; + print "Note: running deposit or withdraw with no arguments will do not +hing, running with only a value will print 'this description is inten +tionaly left blank' in balance.log\n" } sub CheckArg { my $rargs = (shift @_); my $arg; foreach ($rargs) { if ($_ =~ /^-h/) { PrintHelp(); exit 0; } elsif ($_ =~ /^-c/) { Configure(); } else { #must be an undefined option die"Undefined Option: Please run the program with the -h flag +for help\n"; } } } sub Configure { my ($rt, $balance, $transaction); print "Welcome, please follow the instructions to configure this p +rogram (this will overwrite your current configuration)\n"; print "Reoccuring transaction file (rt.log by default)?"; chomp ($rt = <STDIN>); $rt = "rt.log" unless ($rt); #assumes default if user gave +no input. open (WRT, ">$rt"); close (WRT); print "Balance file (balance.log by default)"; chomp ($balance = <STDIN>); $balance = "balance.log" unless ($balance); #assumes default if us +er gave no input. open (WBALANCE, ">$balance") || die "could not open balance.log $ +!"; print WBALANCE "0\n"; close (WBALANCE) || die "could not close balance.log $!"; print "Transaction file (transaction.log by default)"; chomp ($transaction = <STDIN>); $transaction = "transaction.log" unless ($transaction); #assumes d +efault if user gave no input. open (WTRANSACTION, ">$transaction") || die "could not open trans +action.log $!"; print WTRANSACTION "start\n"; close (WTRANSACTION) || die "could not close transaction.log $!"; #write everything into configuration file open (WCONF, ">simplemoney.conf") || die "could not open simplemo +eny.conf $!"; print WCONF "$rt\n$balance\n$transaction"; close (WCONF) || die "could not close simplemoney.conf $!"; } sub CalcBalance { my ($balance, @log); my $location = shift(@_); open (RBALANCE, $location) || die "Can't open $location $!"; @log = <RBALANCE>; close (RBALANCE) || die "Can't close $location $!"; $balance = $log[-1]; if ($_[0]) { # we check to see if there is another # argument (the amount to change) an +d # it is not 0. my $toChange = shift(@_); # we only declare this if another # argument exists to make the test # above work. $balance += $toChange; open (WBALANCE, ">>$location") || die "Can't open $location $!"; print WBALANCE "$balance\n"; close (WBALANCE) || die "Can't close $location $!"; } return $balance; } sub AddTransaction { my ($location) = shift(@_); my ($description) = shift(@_) || "This description is intentionaly + left blank"; open (WTRANSACTION, ">>$location") || die "Can't open $location $! +"; print WTRANSACTION "$description\n"; close (WTRANSACTION) || die "Can't close $location $!"; }

In reply to SimpleMoney by Fingo

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2021-01-24 22:57 GMT
    Find Nodes?
      Voting Booth?