Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

problems editing a flat-file

by Anonymous Monk
on Jul 17, 2001 at 23:23 UTC ( #97421=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Could anyone tell me why this script will not edit the entries from my flat-file database? In my browser I always get to the "records modified" page, however nothing is changed in the DB.
#!/usr/local/bin/perl use CGI; my $query =new CGI; my $guest_file = "/cgi-bin/data/igb-data.ais"; &print_page_start; if ($query->param()) { if ($query->param('new_name')) { if (&valid_form) { eval { open (GUESTS, "+>> $guest_file") or die "Can't open $guest_file: $!"; flock GUESTS, 2; seek GUESTS, 0, 0; my @guests = <GUESTS>; my @new_guests = (); foreach $guest (@guests) { chomp $guest; ($name, $email, $comments) = split ('\|\|'), $guest; if ($name eq $query->param('name') && $email eq $query->param('email') && $comments eq $query->param('comments')) { $name = $query->param('new_name'); $email = $query->param('new_email'); $comments = $query->param('new_comments'); $guest = "$name||$email||$comments"; } local $/ = local $\ = local $, = "vroom"; push @new_guests, $guest; } seek GUESTS, 0,0; truncate GUESTS, 0; print GUESTS @new_guests; close GUESTS; print "<P>Record(s) modified.</P>\n"; print "<A HREF=\"retrieve.pl\">Retrieve records.</A>\n"; } } else { &print_form; } } else { &print_form; } } else { &print_record_list; } chomp $@; if ($@) { print "ERROR: $@<BR>\n"; } &print_page_end; sub print_page_start { print $query->header; print "<HTML>\n<HEAD>\n<TITLE>Modify Records</TITLE>\n"; print "</HEAD>\n<BODY>\n"; print "<H1>Modify Records</H1>\n"; } sub print_form { print "<P>\n<FORM>\n"; if (!$query->param()) { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\"><BR> +\n"; print "VALUE=\"$comments\">$comments<BR>\n"; } else { if ($query->param('new_name')) { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\" "; print "VALUE=\"", $query->param('new_name'), "\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\" "; print "VALUE=\"", $query->param('new_email'), "\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\" + "; print "VALUE=\"", $query->param('new_comments'), "\"><BR>\n +"; } else { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\" "; print "VALUE=\"", $query->param('name'), "\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\" "; print "VALUE=\"", $query->param('email'), "\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\" + "; print "VALUE=\"", $query->param('comments'), "\"><BR>\n"; } } } print "<INPUT TYPE=\"hidden\" NAME=\"name\" "; print "VALUE=\"" . $query->param('name') . "\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"email\" "; print "VALUE=\"" . $query->param('email') . "\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"comments\" "; print "VALUE=\"" . $query->param('comments') . "\">\n"; print "<INPUT TYPE=\"submit\" VALUE=\"submit\">\n"; print "</FORM>\n</P>\n"; sub valid_form { $return_code = 1; if (!$query->param('new_name')) { print "You must enter a name.<BR>\n"; $return_code = 0; } if (!$query->param('new_email')) { print "You must enter an email address.<BR>\n"; $return_code = 0; } if (!$query->param('comments')) { print "You must enter some comments.<BR>\n"; $return_code =0; } return $return_code; sub print_record_list { open (GUESTS, "> $guest_file") or die "Can't open guest file: $!"; while (<GUESTS>) { chomp; ($name, $email, $comments) = split ('\|\|'); print "<P>\n"; print "<FORM>\n"; print "Name: $name<BR>\n"; print "Email: $email<BR>\n"; print "Comments: $comments<BR>\n"; print "<INPUT TYPE=\"hidden\" NAME=\"name\" VALUE=\"$name\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=\"$email\">\n +"; print "<INPUT TYPE=\"hidden\" NAME=\"comments\" VALUE=\"$comment +s\">\n"; print "<INPUT TYPE=\"submit\" VALUE=\"Edit this entry\">\n"; print "</FORM>\n"; print "</P>\n"; } } sub print_page_end { print "</BODY>\n</HTML>\n"; } }

Comment on problems editing a flat-file
Download Code
Re: problems editing a flat-file
by Albannach (Prior) on Jul 17, 2001 at 23:38 UTC
    You might find a few hints if you use strict; and used -w or use warnings; (depending on your version of Perl).

    In your foreach $guest loop your split is not acting on $guest as you have mis-placed your brackets. Because of this, you never find the matching record that you want to change, and so your file is never any different after execution. Warnings would have caught this problem.

    --
    I'd like to be able to assign to an luser

Re: problems editing a flat-file
by aijin (Monk) on Jul 17, 2001 at 23:56 UTC
    There are a bunch of problems with this code.

    First off, your formatting is very difficult to read. You should indent the code like this.

    Example:

    if ($blah) { foreach (@goo) { print; } } else { print "no way man!\n"; }

    It makes it much easier to check that all your braces line up.

    Secondly, you're not sending $query to the valid_code subroutine, or any of the other subroutines that require that information to work properly.

    Thirdly, you don't need to put the code in your first if statement into an eval statement.

    My guess is that you're never getting to the part of your code that updates the records.

    There are a lot of things you need to do to get this script working. If you'd like, msg me, and I'd be happy to guide you in the right direction.

    -aijin

(Ovid - stop asking if you don't want answers) Re: problems editing a flat-file
by Ovid (Cardinal) on Jul 18, 2001 at 00:34 UTC
      Well I can see the flames are spreading through the monastery today..

      Wonder why I'm asking the same questions..because so far very little of the suggestions provided have worked.
      You are right about it being "virtually" the same problem as before, HOWEVER, I am making progress and I am much closer to a solution to this than before
      Wouldn't it have been nice if everyone was very patient with YOU when YOU were first learning perl?

        I've never really noticed the multiple posts and such, so I personally have no hard feeling against you (yet)... but you might start with "use strict", get rid of all the silly mistakes and *then* ask again

        The links that Ovid points out have the same advice for you to use strict. You may want to act out on everybody else's advice first if you want everybody else to be patient.

        I think if you post the same script again without at least doing that much, you're going to get even worse of a reaction...

      I think your answer in this email is extremely rude and you didn't bother to answer the initial question, just post your critiques. I seem to find that throughout this board.

      When someone asks a question for a specific answer, do you think they need to be bogged down with a bunch of criticisms that have nothing to do with the question at hand? The most irritating part is that after reading all the crap that was written, the answer to the original question is rarely there...when someone DOES address a question, the "answer" is usually incomplete.

      This is so counterproductive to someone TRYING to learn. I would recommend that anyone trying to learn the basics of cgi and perl to leave this board and head over to google.com and search for another one.

      This seems to be a board just for cgi.pm propaganda. geez....

      By the way, the person who posted the questions you are referring to in your above post actually posted a DIFFERENT question on the links you're referring to - and THEY WEREN'T ANSWERED EITHER.

      This board and the people who claim to be helping are laughable.

        Nope, read the posts again and you'll see that deriwana was asking essentially the same question.

        Imagine the following. I can't find my car keys, so I ask a friend for help. My friend comes over and sees clothes all over the floor, papers strewn everywhere, and trash piling up. My friend says "How can you find your keys in this mess? Clean your room and it will be easier to find."

        Time passes and I again ask my friend to help me find my keys. What does my friend see? I've now pulled out all of my drawers and strewn even more stuff around the room. When my friend says "I'm not going to help because you didn't clean your room", is my friend being rude or am I inconsiderate for ignoring my friend's advice?

        deriwana was using strict at one point and decided to remove it. Warnings would have helped, but this person ignored advice to use them, also. deriwana was also eval'ing a chunk of vanilla Perl. This person was also asked to provide information from log files, but didn't.

        In short, multiple people pointed out that cleaning the room would make the keys easier to find, but deriwana chose to make the room messier. Read through my posts. You will see that I have spent a considerable amount of time helping people out with many problems. Invariably, if I am a bit short with someone, it's because of issues like this (or if they flame).

        I will happily help someone with a problem that I feel I understand. Heck, I even help them clean their room. I won't, however, clean their room for them. Further, if their room gets messier after being told why they should clean it, I'm not going to help any more.

        Hope that helps ;-)

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        Thank you for this extremely constructive criticism.

        This seems to be a board just for cgi.pm propaganda. geez....

        Geez indeed! We're raking it in hand-over-fist every time we mention CGI.pm here. Ovid himself is simply rolling in it. Fortune magazine has contacted several monks to profile them simply based on the riches made by promoting CGI.pm. :)

        What do you really think? CGI is some sort of weird Amway we're selling here? We recommend it because we find it helped us get our jobs done faster, when our job was to make fairly straightforward interactive web pages.

        I know that when I first came to the Monastery, I hardly understood some of the important discipline necessary for successful (Perl) programming. If you look at some of my recent work you'll see I still have a long way to go. What you won't find is me complaining about suggestions to program carefully , to implement careful controls (like 'use strict'), and pointers to things that can save me time (i.e. "here's the wheel you are trying to invent, you can do something else now"). Why? Because I know that the other monks are looking out for me and my long-term success as a (Perl) programmer.

        Now, I am nowhere near the great programmer that some of my fellow monks are and I'm not always as cheerfully helpful as Ovid is, but I know that for my part I am incredibly glad that PerlMonks exists and that the people here take the time to give out for free the help and advice that they do.

        So if you really want us to change, why not set an example by signing up and answering some of the latest questions yourself? If you have a better way to do CGI than CGI.pm, please fill us in!
(ichimunki) Re: problems editing a flat-file
by ichimunki (Priest) on Jul 18, 2001 at 01:57 UTC
    You should probably consider "use strict" and #!/usr/bin/perl -w. For a web script you should investigate -T as well.

    You should probably consider using the wonderful CGI functions that make HTML happen for you (your code becomes a lot more readable as a Perl script when you do this). This would also allow you to do away with print_start_page and print_page_end.

    The sub valid_form does not appear to be properly closed-- although you have an extra } hanging off the end of the script, which will close it in order to prevent compiler warnings.

    As others have noted, your indentation style is a little obscure-- I suggest finding an editor (see Outside Links) that will automatically assist you in this. I use emacs on both Linux and Windows and find it to be an excellent cross-platform tool.

    Finally, my personal preference would be to replace &subroutine with subroutine() for subroutine calls and to consistently parenthesize all function calls for readability: push( @my_array, $my_new_value );. YMMV.
Re: problems editing a flat-file
by buckaduck (Chaplain) on Jul 18, 2001 at 23:34 UTC
    As in all of the earlier times you asked this question, it would be helpful to know the contents of the web server's error log. Failing that, you could run it from the command line and let us know how that fares.

    At a guess, I wonder if you're having trouble accessing the file /cgi-bin/data/igb-data.ais On most web servers, the cgi-bin directory is not in the server's root directory (even though you might think so from the URL).

    So perhaps the file you're looking for is really: $WWW_HOME/cgi-bin/data/igb-data.ais

    If this is the case, you'd see it as an error message when you try to open the file. If you ever checked for the error messages, that is...

    buckaduck

Re: problems editing a flat-file | my input to your effort
by andr321 (Initiate) on Apr 29, 2008 at 06:20 UTC
    I was searching online for help myself, I found your case. I have trying to work on it. I found out something that is good you can do, if not you will be able to use what I supply to you and you will get your way around.

    It is not a solution, but a tool.

    Change your file name like this:

    my $query =new CGI; # my $guest_file = "/cgi-bin/data/igb-data.ais"; my $guest_file ="andre1.txt"; &print_page_start; if ($query->param()) { if ($query->param('new_name')) { if (&valid_form) { # my $guest_file ="andre1.txt"; eval { open (GUESTS, "+>> $guest_file") or die "Can't open $guest_file: $!"; flock GUESTS, 2; seek GUESTS, 0, 0; my @guests = <GUESTS>; my @new_guests = (); keep it in the same directory till you get everything in order. Your "{" in the sub.... sub print_page_start { print $query->header; print "<HTML>\n<HEAD>\n<TITLE>Modify Records</TITLE>\n"; print "</HEAD>\n<BODY>\n"; print "<H1>Modify Records</H1>\n"; # }
    must go away. This is where you have an outpout with no data in the "Modify Records."

    Create andre1.txt or any file with your fields names and your delimiter you chose:

    Gray Hair |hair@yahoo.com| Wisdom of old man

    Try it. It may delete your file when you click submit, but play with it. That is your first bullet to see something on the screem. Let me know what you come up with.

    Anre is my name.

    I will hear from you in this post.

    I have no idea on your retrieve.pl but if it works fine, it should serve well instead of andre1.txt and my output on the screen. Submit destroy it, but you may continue from there. There is a +'; that I put on coment for you too.

    I am not a programer in perl, I know nothing,but I love to learn and to be around the learners and the experts.

    Here your full code here. copy it and paste it and do : http://127.0.0.1/cgi-bin/data1/data2.pl It should give you a better result.

    #!/usr/bin/perl use CGI; my $query =new CGI; # my $guest_file = "/cgi-bin/data/igb-data.ais"; my $guest_file ="andre1.txt"; &print_page_start; if ($query->param()) { if ($query->param('new_name')) { if (&valid_form) { # my $guest_file ="andre1.txt"; eval { open (GUESTS, "+>> $guest_file") or die "Can't open $guest_file: $!"; flock GUESTS, 2; seek GUESTS, 0, 0; my @guests = <GUESTS>; my @new_guests = (); foreach $guest (@guests) { chomp $guest; ($name, $email, $comments) = split ('\|\|'), $guest; if ($name eq $query->param('name') && $email eq $query->param('email') && $comments eq $query->param('comments')) { $name = $query->param('new_name'); $email = $query->param('new_email'); $comments = $query->param('new_comments'); $guest = "$name||$email||$comments"; } local $/ = local $\ = local $, = "vroom"; push @new_guests, $guest; } seek GUESTS, 0,0; truncate GUESTS, 0; print GUESTS @new_guests; close GUESTS; print "<P>Record(s) modified.</P>\n"; # print "<A HREF=\"retrieve.pl\">Retrieve records.</A>\n"; print "<A HREF=\"retrieve.pl\">Retrieve records.</A>\n"; } } else { &print_form; } } else { &print_form; } } else { &print_record_list; } chomp $@; if ($@) { print "ERROR: $@<BR>\n"; } &print_page_end; sub print_page_start { print $query->header; print "<HTML>\n<HEAD>\n<TITLE>Modify Records</TITLE>\n"; print "</HEAD>\n<BODY>\n"; print "<H1>Modify Records</H1>\n"; # } # sub print_form { print "<P>\n<FORM>\n"; if (!$query->param()) { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\"><BR> +\n"; print "VALUE=\"$comments\">$comments<BR>\n"; } else { if ($query->param('new_name')) { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\" "; print "VALUE=\"", $query->param('new_name'), "\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\" "; print "VALUE=\"", $query->param('new_email'), "\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\" + "; print "VALUE=\"", $query->param('new_comments'), "\"><BR>\n +"; } else { print "Name: <INPUT TYPE=\"text\" NAME=\"new_name\" "; print "VALUE=\"", $query->param('name'), "\"><BR>\n"; print "Email: <INPUT TYPE=\"text\" NAME=\"new_email\" "; print "VALUE=\"", $query->param('email'), "\"><BR>\n"; print "Comments: <INPUT TYPE=\"text\" NAME=\"new_comments\" + "; print "VALUE=\"", $query->param('comments'), "\"><BR>\n"; } } } print "<INPUT TYPE=\"hidden\" NAME=\"name\" "; print "VALUE=\"" . $query->param('name') . "\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"email\" "; print "VALUE=\"" . $query->param('email') . "\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"comments\" "; print "VALUE=\"" . $query->param('comments') . "\">\n"; print "<INPUT TYPE=\"submit\" VALUE=\"submit\">\n"; print "</FORM>\n</P>\n"; sub valid_form { $return_code = 1; if (!$query->param('new_name')) { print "You must enter a name.<BR>\n"; $return_code = 0; } if (!$query->param('new_email')) { print "You must enter an email address.<BR>\n"; $return_code = 0; } if (!$query->param('comments')) { print "You must enter some comments.<BR>\n"; $return_code =0; } return $return_code; sub print_record_list { open (GUESTS, "> $guest_file") or die "Can't open guest file: $!"; while (<GUESTS>) { chomp; ($name, $email, $comments) = split ('\|\|'); print "<P>\n"; print "<FORM>\n"; print "Name: $name<BR>\n"; print "Email: $email<BR>\n"; print "Comments: $comments<BR>\n"; print "<INPUT TYPE=\"hidden\" NAME=\"name\" VALUE=\"$name\">\n"; print "<INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=\"$email\">\n"; # +"; print "<INPUT TYPE=\"hidden\" NAME=\"comments\" VALUE=\"$comment\">\n +"; print "<INPUT TYPE=\"submit\" VALUE=\"Edit this entry\">\n"; print "</FORM>\n"; print "</P>\n"; } } sub print_page_end { print "</BODY>\n</HTML>\n"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://97421]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2014-10-23 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls