in reply to Re: Some one help me with this code.how to make it work.
in thread Some one help me with this code.how to make it work.

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?

Replies are listed 'Best First'.
Re^3: Some one help me with this code.how to make it work.
by yedukondalu (Acolyte) on Apr 08, 2015 at 05:35 UTC

    Sorry for the terrible code i have written..As i am new to perl, with the basic knowledge what i have ,tried to write a code to store the details of an employee into a text file as hash(%),so that i can store the id as a key and all details in a list assigned to the key.so i can access the details with the key when ever i need.. This is what i need..Can u please help me with that..

      I'd be glad to help.

      The code is not terrible, on the surface. Frankly, it really only shows you're new to Perl -- no crime in that.

      What was in question was whether or not the code was yours.

      You still haven't made a convincing argument that it is -- but at least you are now addressing the questions.

      So let's step through this, and see where we end up.

      Let's start with this:

           tried to write a code to store the details of an employee into a text file as hash(%)

      For a newcomer, it is not surprising you simply tried to write the hash into a file directly -- Perl does so many things for us automatically, you might presume it can do this, too.

      Sadly, however, this is not a reliable technique. There are ways to make it reliable, but (get used to this, BTW) there's a question you need to answer first:

      Do you want to use a Module, or write all the code yourself?

      Where we go next depends on your answer.

        There's no wrong answer here. There are a lot of benefits to using a module, but there is a lot to be said for doing it yourself, especially if the goal is to get better at programming in Perl.

        We simply need your decision in order to proceed.

        Thanks for your help.. I am not interested in module..I just need to write my code so that i can understand the concepts where to use and when to use.. <\p>