pinnacle has asked for the wisdom of the Perl Monks concerning the following question:

SCRIPT

#!/usr/bin/perl -w use Checking; $here = <<ATM; 1 deposit 2 withdraw 3 current Balance 4 exit ATM print $here; print "Enter the type of transaction you want to do:"; $enter = <STDIN>; if($enter == '1') { print "Enter the amount you want to deposit:"; $mon = <stdin>; $d = Checking::deposit($mon); print "Deposited Amount: $d\n"; #&deposit($mon); }

Package

package Checking; my $bal = 0; sub deposit { $d = @_; $bal = $bal+$d; # print $bal; #return; } 1;

RESULT

./myATM.pl 1 deposit 2 withdraw 3 current Balance 4 exit Enter the type of transaction you want to do:1 Enter the amount you want to deposit:23 Deposited Amount: 1

I dont understand why in the result its not showing deposited money as 23, instead it always shows 1, I am doing some silly mistake, please assist !!

Replies are listed 'Best First'.
Re: atm deposit problem
by davido (Cardinal) on Jul 20, 2011 at 02:10 UTC

    chomp your input.

    That problem recurs within your script, but isn't the issue.

    Also, deposit() is in a different package (ie, a different namespace). It needs to either be exported into the main:: namespace, or called by its fully qualified name. (Bah, you are calling by its fully qualified name. lol)

    The real problem however is this line: $b = @_; Evaluating an array in scalar context will assign to $b the number of elements in @_. Try $b = shift;, or $b = $_[0].

    Updated to discuss deposit() function.


    Dave

      or, for completeness sake and because it's frequently used:
      ($b) = @_;
      This will be useful once you need more than one value off the list, as you can do
      ($a, $b, $c) = @_;
      Some additional advice: get into the habit of using "strict" and "warnings" in every Perl script you write and declare variables with "my" or "our" (which should be the exception). You can quickly create a mess in a longer script simply with typos in variable names, which are not caught without using "strict". Using "warnings" will help you, among other things, to find variables you are using but have never assigned anything to. What you do in the Perl script is to put this in the top of the script:
      use strict; use warnings;
      Your code tries to keep a balance but there is no way of accessing it. What you are really trying to write is a "Checking" class. BTW: the name seems a bit awkward (it's not checking, it's making a transaction), always try to find a descriptive name for your packages and subroutines, if you can't find one then this might indicate that its task isn't well defined. You could explore object oriented coding to handle this in a better way or let your main script keep the balance. I hope this isn't too confusing.

        It sounds like checking as in "checking account" (or "chequing" to make it less US and more clear).

        Thanks all, your information much appreciated, issue resolved

Re: atm deposit problem
by choroba (Cardinal) on Jul 20, 2011 at 09:40 UTC
    Another problem in your code: The deposit function returns the last expression evaluated: and it is print, which returns 1 when succesfull. Do not print anything in the sub, just return $bal.
Re: atm deposit problem
by Anonymous Monk on Jul 20, 2011 at 22:17 UTC
    When you are having trouble with your homework, ask your teacher.