Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie needs to know why script won't write to a file

Status
Not open for further replies.

ysmith

Programmer
Feb 17, 2005
7
0
0
US
Hi,
I'm relatively new to perl, so my code is a little rough around the edges. I'm trying to create an alumni 'database' for my school and I got my form to send the variables to a perl script, but for some reason, I can't get my script to write the variables to a file...I created a function called writeFile that takes all the variables (right now it's only 1) I want to write. I've tried all kinds of variations on the open ... syntax, but to no avail...does anyone have any idea of what i'm doing wrong?
sub writeFile {
$data_file = "alum2.txt";
open ALUM2, ">>$data_file" or die "cannot open $data_file $!";
flock (ALUM2, 2) || Error('lock', 'file');
print ALUM2 "$_[0]";
close (ALUM2);
}
 
I just ran this ....

#!/usr/bin/perl

&writeFile("hello");

sub writeFile {
$data_file = "alum2.txt";
open ALUM2, ">>$data_file" or die "cannot open $data_file $!";
flock (ALUM2, 2) || Error('lock', 'file');
print ALUM2 "$_[0]";
close (ALUM2);
}

my output file contained:

hello


Seems to be working to me?
 
are you getting any error messages? What does happen when you run writeFile sub?
 
if you are not getting any errors, the seemingly obvious answer is that $_[0] is empty or undefined.

If that is your exact code, then stop, and start using strict

use strict;


it will help you avoid problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top