Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 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. $line =~ s/zid=//;
  44. $zid = $line;
  45. }elsif( $line =~ /program=/){
  46. $line =~ s/program=//;
  47. $program = $line;
  48. }elsif( $line =~ /home_suburb=/){
  49. $line =~ s/home_suburb=//;
  50. $suburb = $line;
  51. }elsif( $line =~ /birthday=/){
  52. $line =~ s/birthday=//;
  53. $birthday = $line;
  54. }elsif ($line =~ /mates=\[/){
  55. $line =~ s/mates=[//;
  56. $line =~ s/]//;
  57. $line =~ s/,/\n/;
  58. $mate =~ $line;
  59. }
  60.  
  61.  
  62.  
  63.  
  64. $details = join '', <$p>;
  65. close $p;
  66. my $next_user = $n + 1;
  67. return <<eof
  68. <div class="matelook_user_details">
  69. $details
  70. </div>
  71. <p>
  72. <form method="POST" action="">
  73. <input type="hidden" name="n" value="$next_user">
  74. <input type="submit" value="Next user" class="matelook_button">
  75. </form>
  76. eof
  77. }
  78.  
  79.  
  80. #
  81. # HTML placed at the top of every page
  82. #
  83. sub page_header {
  84. return <<eof
  85. Content-Type: text/html;charset=utf-8
  86.  
  87. <!DOCTYPE html>
  88. <html lang="en">
  89. <head>
  90. <title>matelook</title>
  91. <link href="matelook.css" rel="stylesheet">
  92. </head>
  93. <body>
  94. <div class="matelook_heading">
  95. matelook
  96. </div>
  97. eof
  98. }
  99.  
  100.  
  101. #
  102. # HTML placed at the bottom of every page
  103. # It includes all supplied parameter values as a HTML comment
  104. # if global variable $debug is set
  105. #
  106. sub page_trailer {
  107. my $html = "";
  108. $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
  109. $html .= end_html;
  110. return $html;
  111. }
  112.  
  113. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement