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

use strict; use warnings; use Data::Dumper; my %database; my @employee_details; my @mykeys; open(FH,"+>>database.txt")|| die 'Cannot open the file'; print FH %database; close FH; sub client { my $choice=shift @_; if($choice eq 'add') { 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; @employee_details=($employee_name, $employee_salary,$employee_ph,"\n +"); $database{$employee_id}=(\@employee_details); print FH %database; } elsif($choice eq 'get') { open(FH,"+>>database.txt")|| die 'Cannot open the file'; print FH my @database; my $mykeys=(\@database); print STDOUT @{$mykeys},"\n"; } } print STDOUT "Enter your option\n"; my $input=<STDIN>; chomp $input; client($input);
I can't get the employee details..

Replies are listed 'Best First'.
Re: Some one help me with this code.how to make it work.
by Corion (Patriarch) on Apr 07, 2015 at 12:23 UTC

    Maybe you can explain line by line what your code is supposed to do?

    elsif($choice eq 'get') { open(FH,"+>>database.txt")|| die 'Cannot open the file'; print FH my @database; my $mykeys=(\@database); print STDOUT @{$mykeys},"\n"; }

    Also, most likely, your course material contains all the information needed for reading and writing from a text file.

    I recommend using DBD::SQLite instead if this is not homework.

      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?

        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..

      sorry for that.. i reopened the file as i can't the content in the file handle.I hope it is not necessary there.

        Which line of the code does your explanation refer to? What about the other lines? Why did you write them if you don't explain them?

Re: Some one help me with this code.how to make it work.
by GotToBTru (Prior) on Apr 07, 2015 at 14:51 UTC

    Does your database.txt file contain any data? Your 'add' logic tries to store it, but the filehandle is closed so nothing can be written. Your 'get' logic opens the file but never reads from it. Even if there was some data in there, you would never see it.

    UPDATE: see File IO Tutorial.

    Dum Spiro Spero
Re: Some one help me with this code.how to make it work.
by Laurent_R (Canon) on Apr 07, 2015 at 17:34 UTC
    You should really explain in plain English and in detail what you are trying to do, because your code does not make much sense in several places. Just an example in the beginning section:
    # ... my %database; my @employee_details; my @mykeys; open(FH,"+>>database.txt")|| die 'Cannot open the file'; print FH %database; close FH;
    You declare the %database hash, then open a file in read/append mode and print the hash to the file handler. But %database has just been defined and has not been populated at this point, so that you are printing empty content to the file (besides the fact that printing directly a hash to a file might not be the best).

    Similarly, the elsif block ("get") does not make more sense.

    Also I would strongly advise you to indent your code properly. This is very important for understanding the logic of a program.

    Je suis Charlie.