Your program does two things. It prints HTML output (a form you need to fill in) and then it appends to a data file. But you should only be appending to the data file after the form has been submitted. And your FORM tag has a very weird URL: "http://localhost/newuser.cgi?action=newuser?first?last" doesn't make a lot of sense. You probably just want "http://localhost/newuser.cgi".
Here is a simple program:
#!/usr/bin/perl
use CGI;
use CGI::Carp qw( fatalsToBrowser );
use strict;
use warnings;
my $query = CGI->new;
# if we got here from a form being submitted,
# do the adduser() stuff
adduser($query) if $query->param;
print << "END_HTML";
<html>
<body>
<form method="post" action="thisprogram.cgi">
Username: <input type="text" name="user"><br>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" value=" Add User ">
</form>
</body>
</html>
END_HTML
sub add_user {
my ($q) = @_;
open PASSWD, ">> $passwd_file"
or die "can't append to $passwd_file: $!";
print PASSWD join(":" =>
$q->param('first'), $q->param('last'), $q->param('user'), $q->para
+m('pass')
), "\n";
close PASSWD;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.