I've taken the liberty of annotating the code
# -----[ Help Me Get It Right ]--------------------------------------- +-------- use strict; use warnings; # -----[ Modules ]---------------------------------------------------- +-------- use Data::Dumper; # -----[ Globals ]---------------------------------------------------- +-------- my %database; my @employee_details; my @mykeys; # -----[ Kill any database I might already have ]--------------------- +-------- open(FH,"+>>database.txt")|| die 'Cannot open the file'; print FH %database; close FH; # ----[ Perl isn't confusing enough so put subroutine in the middle of + main ]- sub client { # -----[ Blindly accept anything they throw at me ]--------------- +-------- my $choice=shift @_; if($choice eq 'add') { # -----[ Get some data from the user ]------------------------ +-------- print STDOUT 'Enter employee details',"\n"; print STDOUT 'Enter employee id',"\n"; my $employee_id=<STDIN>; chomp $employee_id; print STDOUT 'Enter employee name',"\n"; my $employee_name=<STDIN>; chomp $employee_name; print STDOUT 'Enter employee salary',"\n"; my $employee_salary=<STDIN>; chomp $employee_salary; print STDOUT 'Enter employee phone number',"\n"; my $employee_ph=<STDIN>; chomp $employee_ph; # -----[ Shove well-defined data into a mysterious array ]---- +-------- @employee_details=($employee_name, $employee_salary,$employee_ +ph,"\n"); # -----[ Shove mysterious array into a mysteriouser hash ]---- +-------- $database{$employee_id}=(\@employee_details); # -----[ Now (try to) print to a File Handle I closed in main +]------- # -----[ If I do this enough, maybe Perl won't notice +]------- print FH %database; } elsif($choice eq 'get') { # -----[ Open the file I emptied in main ]-------------------- +-------- open(FH,"+>>database.txt")|| die 'Cannot open the file'; # -----[ Empty it again just to be sure it's dead ]------ +-------- # -----[ Make a new empty array while we're at it ]------ +-------- print FH my @database; # -----[ Fetch the address of the empty array I just created ] +-------- my $mykeys=(\@database); # -----[ Fancy way to print a blank line ]-------------------- +-------- print STDOUT @{$mykeys},"\n"; } } # -----[ Okay, let's continue our original train of thought ]--------- +-------- # -----[ We could have given the user a clue here about what to en +ter ]-- # -----[ But that would take the fun out of it + ]-- # -----[ We could give no prompt at all, but they might just sit t +here ]-- # -----[ Let's at least throw them a bone so they know they are ch +oking]-- # ----=[ Besides, only trained people will use this so do not expl +ain ]-- print STDOUT "Enter your option\n"; # -----[ Get the user "option" ]-------------------------------------- +-------- my $input=<STDIN>; chomp $input; # -----[ Now let's go back and call the subroutine with whatever we go +t ]----- client($input); # -----[ Disappear without ceremony ]--------------------------------- +--------
So, let's see what it does.
D:\PerlMonks>emp1.pl Enter your option add Enter employee details Enter employee id 1 Enter employee name Mike Enter employee salary 15 Enter employee phone number 123456789 print() on closed filehandle FH at D:\PerlMonks\emp1.pl line 55, <STDI +N> line 5. D:\PerlMonks>
Darn, Perl caught me. Again. I wonder how long it will take before it slips by?
In reply to Re^2: Some one help me with this code.how to make it work.
by marinersk
in thread Some one help me with this code.how to make it work.
by yedukondalu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |