Return value from nested Subroutine Perl -
we've received assignment school, we're supposed make our own small n' simple perl application. thought i'd make atm simulator. far, it's been going great; i've created menu (withdraw, balance, transfer), using subroutines. code far:
#! /usr/bin/perl #written by: tobias svenblad, h15tobsv@du.se, digitalbrott & e-säkerhetsprogrammet (2015) #perllab03-2c.plx use warnings; use strict; use term::ansicolor; use text::format; $firstname; $lastname; $acc_balance = 2451.26; $acc_withdraw; $clr_scr = join( "", ( "\033[2j", "\033[0;0h" ) ); #this variable clear screen , jump postion 0, 0. $atm = text::format->new; print color('green'); print $atm->center("atm v. 1.20"); print color('reset'); #create account message. $crt_acc_msg = <<"end_msg"; \ndear sir or madam,\n we're happy you've chose bank. before proceed, need set-up account.\n end_msg print $crt_acc_msg; &acc_create; &acc_choose; sub acc_create { acc_beginning: #first name: print "\nyour first name: "; $firstname = <stdin>; chomp $firstname; #last name: print "\nyour last name: "; $lastname = <stdin>; chomp $lastname; if ( defined($firstname) && $firstname ne "" ) { if ( defined($lastname) && $lastname ne "" ) { goto acc_pass; } } else { print "you didn't fill in first or last name. try again. \n"; goto acc_beginning; } acc_pass: print "please wait while system loads.\n\n"; #sleep(2); print $clr_scr; print color('green'); print $atm->center("atm v. 1.20"); print color('reset'); print "\nwelcome ", $firstname, " ", $lastname, "!\n\n"; } sub acc_choose { sub acc_balance { print $clr_scr; print color('green'); print $atm->center("atm v. 1.20"); print color('reset'); print "\nyour balance is: "; print color('green'); print $acc_balance; print color('reset'); print " sek\n\n"; &acc_choose; } sub acc_withdraw { enter_amount: print $clr_scr; print color('green'); print $atm->center("atm v. 1.20"); print color('reset'); print "\nenter how you'd withdraw: \n"; $acc_balance_withdraw = <stdin>; if ( $acc_balance_withdraw > $acc_balance ) { print "insufficient funds."; goto enter_amount; } $acc_balance -= $acc_balance_withdraw; print "\nyour current balance now: "; print color('green'); print $acc_balance; print color('reset'); print " sek\n\n"; &acc_choose; } sub acc_transfer { enter_amount: print $clr_scr; print color('green'); print $atm->center("atm v. 1.20"); print color('reset'); print "\nenter how you'd transfer: \n"; $acc_balance_withdraw = <stdin>; if ( $acc_balance_withdraw > $acc_balance ) { print "insufficient funds."; goto enter_amount; } print "\nyour current balance now: "; print color('green'); print $acc_balance - $acc_balance_withdraw; print color('reset'); print " sek\n\n"; &acc_choose; } acc_choose: print "[ "; print color('cyan'); print "1"; print color('reset'); print " ]"; print "account balance\n"; print "[ "; print color('cyan'); print "2"; print color('reset'); print " ]"; print "withdraw\n"; print "[ "; print color('cyan'); print "3"; print color('reset'); print " ]"; print "transfer\n"; $choice1 = <stdin>; chomp $choice1; if ( $choice1 == 1 ) { &acc_balance; } elsif ( $choice1 == 2 ) { &acc_withdraw; } elsif ( $choice1 == 3 ) { &acc_transfer; } else { print "you entered invalid option. try again. \n"; goto acc_choose; } return (); }
the problem face when try return $acc_balance
value other subroutines. i've tried implement return($acc_balance );
underneath nested subroutines, that'll prompt me end application. basically, i'm trying update $acc_balance
every time make withdrawal or transfer (they both same thing in code), whenever try that, either doesn't update value or it'll show classic "press key continue..."
message.
any appreciated! thanks!
i think shouldn't using subroutines assignment. worries me has told use ampersand &
when call subroutine , has explained how use labels , goto
. that's inappropriate modern computer language , can better
for instance, here's how write subroutine acc_create
sub acc_create { while () { print "\nyour first name: "; chomp (my $firstname = <stdin>); print "\nyour last name: "; chomp (my $lastname = <stdin>); last if $firstname , $lastname; print "you didn't fill in first or last name. try again.\n"; } print "please wait while system loads.\n\n"; print $clr_scr, color('green'), $atm->center("atm v. 1.20"), color('reset'); print "\nwelcome $firstname $lastname!\n\n"; }
there's lot more say, stack overflow isn't place tutorial
Comments
Post a Comment