ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am building a simple program in perl that takes information from a web page form I have and appends it to a file(database). The program is just supposed to add the id number # last name # first name from the information I enter on the webpage. I can not get it to write the the file. I am trying to learn how to write to files and pull info from them.
Note: no changes are ever made to the file. except for once there were to # signs.
I can not figure out my bug can anyone point me in the right direction.
Code:
#!/usr/bin/perl
print "Content-type\:text/html\n\n";
print "<html>";
print "<head><title>New Bowler Added</title></head>";
print "<body bgcolor=yellow text=brown>";
$str=$ENV{QUERY_STRING};
($str1, $str2, $str3) = split (/&/, $str);
($discard, $id) = split (/=/, $str1);
($discard, $fname) = split (/=/, $str2);
($discard, $lname) = split (/=/, $str3);
#Do not get this in the notes it is like this but it is compiling with errors
#open file ">bowl1.dat";
open (file, ">bowl1.dat");
print (file "$id#$fname#$lname\n");
close (file);
print "<p><h3>You just added $fname $lname with ID number $id to your bowling database.</h3></p>";
print "</body>";
print "</html>";
I tried that already. There is something wrong with the print statement. I can print to the screen but it is not printing to the file. The file stays the same. Any other suggestions or Mods I should make.
#!/usr/bin/perl
use warnings;
use strict;
#Filename
my $datfile = "bow11.dat";
open(HANDLE, ">$datfile")||die "Opening $datfile : $!";
print HANDLE "$id#$fname#$lname\n";
close(HANDLE);
File handles should always be in CAPITOLS to avoid conflicting with perl names etc. Also you might consider using CGI.pm to make it easier to output html and you can then use a form (much better) then instead of passing info using a query string.
Upper case is merely a convention. A *good* convention to follow ... but certainly not a requirement.
And the spelling is "capital" ;-)
All -
Dumb question on my part: but have you guys checked file permissions yet? Specifically, does the Apache user have write permission on the file (and perhaps directory) in question?
The posts from paulsm4 and cramer point you in the correct direction, if not now, then certainly for the future.
It is a rare script that uses open but does not check for the return status. In fact, in Damian Conway's Perl Best Practices, he says that all open, close, and print statements should be checked. I think most people do not check after each print, but that may depend on how important your code is.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.