Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- # written by andrewt@cse.unsw.edu.au September 2016
- # as a starting point for COMP2041/9041 assignment 2
- # http://cgi.cse.unsw.edu.au/~cs2041/assignments/matelook/
- use CGI qw/:all/;
- use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
- sub main() {
- # print start of HTML ASAP to assist debugging if there is an error in the script
- print page_header();
- # Now tell CGI::Carp to embed any warning in HTML
- warningsToBrowser(1);
- # define some global variables
- $debug = 1;
- $users_dir = "dataset-medium";
- print user_page();
- print page_trailer();
- }
- #
- # Show unformatted details for user "n".
- # Increment parameter n and store it as a hidden variable
- #
- sub user_page {
- my $n = param('n') || 0;
- my @users = sort(glob("$users_dir/*"));
- my $user_to_show = $users[$n % @users];
- my $details_filename = "$user_to_show/user.txt";
- open my $p, "$details_filename" or die "can not open $details_filename: $!";
- while ($line) = <$p>) {
- if ($line =~ /email=/ || $line =~ /password=/ || $line =~ /home_latitude=/ || $line =~ /home_longitude=/ || $line =~ /courses=/ ) {
- #do nothing
- }
- elsif ( $line =~ /zid=/){
- $line =~ s/zid=//;
- $zid = $line;
- }elsif( $line =~ /program=/){
- $line =~ s/program=//;
- $program = $line;
- }elsif( $line =~ /home_suburb=/){
- $line =~ s/home_suburb=//;
- $suburb = $line;
- }elsif( $line =~ /birthday=/){
- $line =~ s/birthday=//;
- $birthday = $line;
- }elsif ($line =~ /mates=\[/){
- $line =~ s/mates=[//;
- $line =~ s/]//;
- $line =~ s/,/\n/;
- $mate =~ $line;
- }
- $details = join '', <$p>;
- close $p;
- my $next_user = $n + 1;
- return <<eof
- <div class="matelook_user_details">
- $details
- </div>
- <p>
- <form method="POST" action="">
- <input type="hidden" name="n" value="$next_user">
- <input type="submit" value="Next user" class="matelook_button">
- </form>
- eof
- }
- #
- # HTML placed at the top of every page
- #
- sub page_header {
- return <<eof
- Content-Type: text/html;charset=utf-8
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>matelook</title>
- <link href="matelook.css" rel="stylesheet">
- </head>
- <body>
- <div class="matelook_heading">
- matelook
- </div>
- eof
- }
- #
- # HTML placed at the bottom of every page
- # It includes all supplied parameter values as a HTML comment
- # if global variable $debug is set
- #
- sub page_trailer {
- my $html = "";
- $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
- $html .= end_html;
- return $html;
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement