LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2006, 04:34 AM   #1
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Rep: Reputation: 15
appending a file


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

Code:
0001#B#Tom#911#9#114
0002#T#Mark#625#3#231
0003#F#Tony#0#0#0
0004#S#Evan#125#3#52
 
Old 09-30-2006, 04:34 AM   #2
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
sorry that second code block is the file it is writing to.
 
Old 09-30-2006, 10:09 AM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
open (file, ">bowl1.dat");
print (file "$id#$fname#$lname\n");
close (file);
Try:
Code:
open(FILE, ">> bowl1.dat");
print FILE "$id#$fname#$lname\n";
close(FILE);
">" will overwrite your file.
">>" will append to it...
By convention, Perl uses UPPERCASE for file handles.
 
Old 09-30-2006, 03:17 PM   #4
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
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.
 
Old 09-30-2006, 10:39 PM   #5
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
You could try using the IO::File module instead. It makes manipulating files somewhat easier.

http://search.cpan.org/search?mode=m...query=IO::File
 
Old 09-30-2006, 11:06 PM   #6
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Code:
#!/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.

Last edited by cramer; 09-30-2006 at 11:09 PM.
 
Old 09-30-2006, 11:12 PM   #7
ciotog
Member
 
Registered: Mar 2004
Location: Canada
Distribution: Slackware current
Posts: 728
Blog Entries: 2

Rep: Reputation: 43
What error messages are you getting, anyway?
 
Old 10-01-2006, 12:29 AM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
cramer -

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?

Just a thought .. PSM
 
Old 10-01-2006, 11:00 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, circuit_girl.

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.

Best wishes ... cheers, makyo
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 08:42 AM
SSL VPN appending /etc/hosts file zajelo3 Linux - Security 0 02-06-2005 06:31 PM
Appending a string to a file JStew Linux - General 13 03-04-2003 03:48 PM
Appending current date and time to a file frankietomatoes Linux - General 5 11-18-2002 02:09 PM
sending a mail appending a file saavik Linux - Newbie 4 07-12-2002 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration