http://www.perlmonks.org?node_id=97421

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
(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
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: problems editing a flat-file
by Albannach (Monsignor) 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

(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"; } }