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

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.

Replies are listed 'Best First'.
Re^2: Some one help me with this code.how to make it work.
by marinersk (Priest) on Apr 07, 2015 at 21:48 UTC

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

        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.

Re^2: Some one help me with this code.how to make it work.
by yedukondalu (Acolyte) on Apr 07, 2015 at 12:43 UTC

    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?

        In elsif case i am trying to move the reference of the list and trying to print it. I am not aware what code should i write in the elsif case to extract the information of a specific id. <\p>