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

Title:
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, details, 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, summary, 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.