Jump to content

Perl - Editing a text file


kayla

Recommended Posts

It has been a few years since I coded in PERL, but in order to help you I need examples of what you have / want. IE:

 

I have this text file:

text.txt

1
2
3
4

 

And want to replace 2 with two.

 

Without a guide like that it is very difficult to help you.

Hi,

Sorry for the very late reply, my week has been a bit busy.

 

So basically, users would register using this form:

 

<html>
<title>Registration</title>
<body>
<form method="post" action="register.pl">
<center>
<table width="500" border=2 bordercolor="#FFFFFF" bgcolor="#CDCDCD"
  cellpadding="4" cellspacing="0" style="font-size: xx-small; font-family: Verdana;">
<tr>
    <td colspan="2" bgcolor="#FFFFFF"><img src="stc.jpg"></td>
</tr>
<tr>
    <td colspan="2" bgcolor="#6495ED"><b><h1>Register Here</h1></b></td>
</tr>
<tr>
    <td align="left"><p>Forename:</td>
    <td>
    <input type="text" name="forename" size=30> 
    </td>
</tr>
<tr>
    <td align="left"><p>Surname:</td>
    <td>
    <input type="text" name="surname" size=30> 
    </td>
</tr>
<tr>
    <td align="left"><p>Email Address:</td>
    <td>
    <input type="text" name="email" size=30> 
    </td>
</tr>
<tr>
    <td align="left"><p>Password:</td>
    <td>
    <input type="text" name="password" size=30> 
    </td>
</tr>

<tr>
    <td colspan=2 align="right">
    <input type="submit" value="Register">
    </td>
</tr>
</table>
</center>
</form>
</body>
</html>

 

This is the perl file that processes what has been entered in the form:

 

#!/usr/bin/perl -wT
use CGI qw (:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
print header;
print start_html("Registration");
print h2("Registration");

my $email = param('email');
my $forename = param('forename');
my $surname = param('surname');
my $password = param('password');

open(OUTF, ">>var/www/81646/userinfo.txt") or dienice("Couldn't open userinfo.txt for writing: $!");

# lock file
flock(OUTF,2);
# reset file to end incase someone has wrote to it whilst waiting for lock
seek(OUTF,0,2);
print OUTF "Forename: $forename\n";
print OUTF "Surname: $surname\n";
print OUTF "Email: $email\n";
print OUTF "Password: $password\n";
print OUTF "<hr align='left' width='200'>";

close(OUTF);

print h2("Thank You");
print p("You details have been registered. Click <a href='registration.html'>here</a> to return to the registration page.");
print end_html;

sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit
}

 

This is then stored to a text file called userinfo.txt.

This file can be then be viewed in the browser using this file:

 

#!/usr/bin/perl -wT
use CGI qw (:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
print header;
print start_html("Registration");
print h2("Registration");

open(INF,"var/www/81646/userinfo.txt") or dienice("Couldn't open userinfo.txt for reading: $! \n");
my @data = <INF>;
close(INF);

print "<hr>\n";

foreach my $i (@data) {
print "$i\n";
}
print "<br><br><a href='registration.html'>Add a new student</a>;

sub dienice{
my ($errmsg) = @_;
print "<h3>Error</h3>\n";
print "<p>$errmsg</p>\n";
exit;
}
print end_html;

 

All that works perfectly fine.

What I want to do is to create another page that will allow, say an admin, to edit any information in the text file. I have been informed by my lecturer that it may be possible to do this by displaying all the info in the text file in a text area box on a form but we have not learnt how to do this at all and i can't find anything on google.

I know how to edit using hardcoded data, but thats not what I want to do.

 

Any help would be much appreciated.

x

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.