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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl creating files

Status
Not open for further replies.

mike101

Programmer
Jul 20, 2001
169
US
Hey everyone. I need to make it so that a perl script can create a text file so that when a user signs up it will make a txt file named username.txt with username of course the username part being the username (duh lol). But I just need the script to make that and it will be added to the sign-up script and called as a sub. If you can just show me a script that can do this I can modify it to put it into my script. Thanks everyone.
 
Here ya go:

[tt]

sub adduser {
open(FILE,">>username.txt") || die("failed to open file: $!");
flock(FILE);
print FILE $FORM{'username'}."|";
print FILE $FORM{'password'};
close(FILE);
}

[/tt]

The code does this:

Opens the file, creates a new one if it doesn't exist, or appends to the file if it exists. It dies if something went wrong and reports the line number on which it did.
Then you lock the file so that it can't be opened more than once. Then you print to the file using the filehandle that you defined in the open() function. The %FORM hash should be changed to whatever you have on your site (assuming you are not using CGI.pm). Then, after you are done with using the file, you close it.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
So if I want to have the script only create a file without actually writing to it I just get rid of these 2 lines?

print FILE $FORM{'username'}."|";
print FILE $FORM{'password'};

Or what? I just need it to create a file and I can put a variable in as the name of the file to be created which will be called $user but I want the new file blank. Thanks for your help if you can please help me out again that would be perfect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top