Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.19 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # written by andrewt@cse.unsw.edu.au September 2016
  4. # as a starting point for COMP2041/9041 assignment 2
  5. # http://cgi.cse.unsw.edu.au/~cs2041/assignments/matelook/
  6.  
  7. use CGI qw/:all/;
  8. use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
  9.  
  10.  
  11. sub main() {
  12.     # print start of HTML ASAP to assist debugging if there is an error in the script
  13.     print page_header();
  14.    
  15.     # Now tell CGI::Carp to embed any warning in HTML
  16.     warningsToBrowser(1);
  17.    
  18.     # define some global variables
  19.     $debug = 1;
  20.     $users_dir = "dataset-medium";
  21.    
  22.     print user_page();
  23.     print page_trailer();
  24. }
  25.  
  26.  
  27. #
  28. # Show unformatted details for user "n".
  29. # Increment parameter n and store it as a hidden variable
  30. #
  31. sub user_page {
  32.     my $n = param('n') || 0;
  33.     my @users = sort(glob("$users_dir/*"));
  34.     my $user_to_show  = $users[$n % @users];
  35.     my $details_filename = "$user_to_show/user.txt";
  36.     open my $p, "$details_filename" or die "can not open $details_filename: $!";
  37.  
  38.     while ($line) = <$p>) {
  39.     if ($line =~ /email=/ || $line =~ /password=/ || $line =~ /home_latitude=/ || $line =~ /home_longitude=/ || $line =~ /courses=/ ) {
  40.         #do nothing
  41. }
  42.     elsif ( $line =~ /zid=/){
  43.        
  44.     }
  45.  
  46.  
  47.  
  48.     $details = join '', <$p>;
  49.     close $p;
  50.     my $next_user = $n + 1;
  51.     return <<eof
  52. <div class="matelook_user_details">
  53. $details
  54. </div>
  55. <p>
  56. <form method="POST" action="">
  57.     <input type="hidden" name="n" value="$next_user">
  58.     <input type="submit" value="Next user" class="matelook_button">
  59. </form>
  60. eof
  61. }
  62.  
  63.  
  64. #
  65. # HTML placed at the top of every page
  66. #
  67. sub page_header {
  68.     return <<eof
  69. Content-Type: text/html;charset=utf-8
  70.  
  71. <!DOCTYPE html>
  72. <html lang="en">
  73. <head>
  74. <title>matelook</title>
  75. <link href="matelook.css" rel="stylesheet">
  76. </head>
  77. <body>
  78. <div class="matelook_heading">
  79. matelook
  80. </div>
  81. eof
  82. }
  83.  
  84.  
  85. #
  86. # HTML placed at the bottom of every page
  87. # It includes all supplied parameter values as a HTML comment
  88. # if global variable $debug is set
  89. #
  90. sub page_trailer {
  91.     my $html = "";
  92.     $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
  93.     $html .= end_html;
  94.     return $html;
  95. }
  96.  
  97. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement