Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Select, display, edit job opportunities application
Perl on the Web example from a Well House Consultants training course
More on Perl on the Web [link]
Source code: job3.pl Module: P221
#!/usr/bin/env perl

=head1 Select and display job vacancies

This is a "proof of concept" demo of a system for
display and management of job opportunities stored
in a text file.

User is initially given the option of selecting a
summary of all jobs, a listing of places at which
jobs are available, and an edit screen.

The summary of all jobs, if requested, produces the
report and repeats the initial form.

The request for all jobs at a location displays a
list of location and when one is selected moves on
to display all appropriate vacancies in detail.

The edit screen asks the user to enter a refernce
of a job for edit, which is then called up for
alteration as appropriate. When the data is called
up in this way, the separate options for delete and
insert allow for jobs to be removed, and for a new
job to be added rather than a current one replaced.

A password is required in order to call up a job for
edit, and a confirmation must be entered if a job is
to be deleted or a new one inserted.

The script is "secure" as far as viewers are concerned -
in other words, they can only select from pulldown
menus and can do no harm to the underlying data. Data
editors, however, need to be careful as checking is
minimal in this "proof of concept".

=cut


sub collect_form {

if ($ENV{"REQUEST_METHOD"} eq "POST") {
        read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"});
        $form{"method"} = "POST";
} else {
        $buffer = $ENV{QUERY_STRING};
        $form{"method"} = "GET";
}

@fof = split(/&/,$buffer);
foreach $field(@fof) {
        ($name,$value) = split(/=/,$field);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9]{2})/pack("C",hex($1))/eg;
        if ($form{$name}) {
                $form{$name} .= "\n$value";
        } else {
                $form{$name} = $value;
                }
        }
}

collect_form();

$topform = <<"NF";
Please select action<br>
<input name=dowhat type=radio value=display> Choose Jobs<br>
<input name=dowhat type=radio value=summary> Summarise Jobs<br>
<input name=dowhat type=radio value=edit> Edit Jobs - authorised users only<br>
<input name=pageno type=hidden value=1>
NF

if ($form{pageno} < 1) {
        $stuff = "Your results will appear here";
        $nextform = $topform;
} elsif ($form{pageno} == 1 and $form{dowhat} eq "summary") {
        $stuff = "Here is your summary";
        open (FH,"jobs.txt");
        $stuff .= "<table border=1>";
        foreach $hlines (grep (/^\*/,<FH>)) {
                $hlines =~ s/.*?\t//;
                $hlines =~ s/\t/<\/td><td>/g;
                $stuff .= "<tr><td>$hlines</td></tr>\n";
                }
        $stuff .= "</table>";
        $nextform = $topform;
} elsif ($form{pageno} == 1 and $form{dowhat} eq "display") {
        $stuff = "Here is a list of places";
        $nextform = "<input name=pageno type=hidden value=2>";
        open (FH,"jobs.txt");
        foreach $hlines (grep (/^\*/,<FH>)) {
                ($star,$place) = split(/\t/,$hlines);
                $plcount{$place}++;
                }
        foreach $venue(sort (keys(%plcount))) {
                $what = ($plcount{$venue} == 1) ? "job" : "jobs";
                $nextform .= "<input name=venue type=radio value=$venue> at $venue ".
                        "($plcount{$venue} $what listed)<br>";
                }
} elsif ($form{pageno} == 1 and $form{dowhat} eq "edit") {
        $stuff = "Select job reference for this";
        $nextform = "<input name=pageno type=hidden value=100>";
        $nextform .= "<input name=refcode size=4> Job Code<br>";
        $nextform .= "<input name=arthur size=8 type=password> password<br>";
} elsif ($form{pageno} == 1 ) {
        $stuff = "YOU GOTTA PICK AN OPTION, BOYO!";
        $nextform = $topform;
} elsif ($form{pageno} == 2) {
        $stuff = "Details of jobs on offer at $form{venue}";
        open (FH,"jobs.txt");
        foreach $hlines (<FH>) {
                ($star,$place,$rest) = split(/\t/,$hlines,3);
                if ($star eq "*") {
                        if ($place eq $form{venue}) {
                                $stuff .= "<hr><b>$rest</b><br>";
                                $inthejob = 1;
                        } else {
                                $inthejob = 0;
                        }
                } else {
                        if ($inthejob) {
                                $stuff .= "$hlines\n";
                        }
                }
        }
} elsif ($form{pageno} == 100) {
        if ($form{arthur} ne "abc123") {
                $stuff = "Bad password - your IP reported";
                $nextform = $topform;
        } else {

=head1 data looks like this

* Hemel Bottle Washer 20000 EH18
All about the job which is going to be
a number of lines long and so on

=cut


        $stuff = "Please make your changes";
        open (FH,"jobs.txt");
        foreach $hlines (<FH>) {
                chop ($hlines);
                ($star,$place,$role,$salary,$ref) = split(/\t/,$hlines);
                if ($star eq "*") {
                        if ($form{refcode} eq $ref) {
                        # At this point, make up an edit form!
                        $nextform = "<input name=pageno type=hidden value=101>";
                        $nextform .= "<input name=ref type=hidden value=$ref> Ref $ref<br>";
                        $nextform .= "<input name=newjob> Type new no. for INSERT<br>";
                        $nextform .= "<input name=remove> Type REMOVE to delete<br>";
                        $nextform .= "<input name=confirm> Type YES to confirm insert or delete<br>";
                        $nextform .= "<br><input name=salary value='$salary'> Salary<br>";
                        $nextform .= "<input name=role value='$role'> Role<br>";
                        $nextform .= "<input name=place value='$place'> Place<br>";
                        $nextform .= "<textarea name=descript rows=10 cols=40>";
                        $inthejob = 1;
                        $open = 1;
                        } else {
                                $inthejob = 0;
                                if ($open == 1) {
                                        $nextform .= "</textarea>";
                                }
                                $open = 0;
                        }
                } else {
                        if ($inthejob) {
                                $nextform .= "$hlines\n";
                        }
                }

                }
                if ($open == 1) {
                        $nextform .= "</textarea>";
                        }
                $nextform .= "<br>";
        }

} elsif ($form{pageno} == 101) {

        $action = "";
        if ($form{confirm} eq "YES" and $form{remove} eq "REMOVE") {
                $action = "del"; }
        if ($form{confirm} eq "YES" and $form{newjob} ne "") {
                $action = "ins"; }

        # add validation code!!
        if ($action ne "ins") {
        open (FHO,">newjobs.txt");
        open (FH,"jobs.txt");
        foreach $hlines (<FH>) {
                chop ($hlines);
                ($star,$place,$role,$salary,$ref) = split(/\t/,$hlines);
                if ($star eq "*") {
                        if ($form{ref} eq $ref) {
                                if ($action ne "del") {
                                print FHO "*\t$form{place}\t$form{role}\t$form{salary}\t".
                                        "$form{ref}\n";
                                $form{descript} =~ s/\n+$//;
                                print FHO "$form{descript}\n\n";
                                        }
                                $indesk = 1;
                        } else {
                                print FHO "$hlines\n";
                                $indesk = 0;
                        }
                } else {
                        # To add - save new description
                        if ($indesk == 0) {print FHO "$hlines\n"};
                }
        }
        close FHO;
        close FH;
        rename("newjobs.txt","jobs.txt");
        $stuff = "I've done that to $form{ref}!";
        } else {
                $stuff = "Inserted $form{newjob}!";
                open (FHO,">>jobs.txt");
                print FHO "*\t$form{place}\t$form{role}\t$form{salary}\t".
                        "$form{newjob}\n";
                $form{descript} =~ s/\n+$//;
                print FHO "$form{descript}\n\n";
        }
        $nextform = $topform;

} else {

}

$debug = join(", ",keys(%form));

print <<"DONE";
Content-type: text/html

<html>
<body>
<h1>Title</h1>
$stuff
<hr>
<form method=POST>
$nextform
<input type=submit>
</form>
<hr>
$debug
</body>
</html>

DONE

=head1

Following stages / actions on this script in the short(er) term ...

1. Check that jobs called up for edit really do exist
2. Deal with special characters such as < " and ' in any field
3. Deal with data lines in the description starting with "*"
4. Switch to using a MySQL or other database
5. Use a separate file for the template
6. Use cookies / shopping cart to carry more data through
7. Structure code to place data handlers in subs in
   a separate business logic file.

Follow up stages on project might include ...

a. add more fields
b. add date dependent information / code
c. provide ability to do muliple edits without re-login

=cut



Learn about this subject
This module and example are covered on the following public courses:
 * Perl Extra
 * Perl bootcamp
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Perl are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Perl on the Web" training module. You'll find a description of the topic and some other closely related examples on the "Perl on the Web" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author
This web site is written and maintained by Well House Consultants.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb