Advertisement
Guest User

Untitled

a guest
Sep 10th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.44 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use Mojolicious::Lite;
  4. use Encode;
  5.  
  6. my $charset = 'ISO-8859-1';
  7.  
  8. any '/' => sub {
  9.     my $self = shift;
  10.  
  11.     # Render template "root/index.html.ep" with message
  12.     $self->render(
  13.         message => 'Global charset is <b>' . $charset .
  14.                    '</b>,<br />Mojo::Parameters->charset is <b>' .
  15.                    $self->req->params->charset . '</b><br />',
  16.         params => $self->req->params->to_hash,
  17.     );
  18. } => 'index';
  19.  
  20. 1;
  21.  
  22. plugin Charset => {charset => $charset};
  23.  
  24. app->start;
  25.  
  26. __DATA__
  27.  
  28. @@ index.html.ep
  29. <!doctype html><html>
  30. <head><title>Encoding Test</title></head>
  31. <body>
  32. <p><%== $message %></p>
  33.  
  34. <% if (%$params) { %>
  35. <h3>Dump parameters</h3>
  36. <p><blockquote><pre><%= dumper $params %></pre></blockquote></p>
  37.  
  38. <h3>Character string lengths</h3>
  39. <p><blockquote><pre><%
  40.   for (sort keys %$params) {
  41.     %><%= qq'$_: "$params->{$_}", length: ' . length($params->{$_}) . ", has UTF-8 flag: " . (Encode::is_utf8($params->{$_}) ? "yes" : "no") . "\n" %><%
  42.   } %></pre></blockquote></p>
  43. <% } else { %>
  44. <p>Please specify HTTP parameters with with some characters above the 7bit-ASCII range.</p>
  45.  
  46. <p>I.e. start with
  47. <blockquote><a href="http://127.0.0.1:3000/?p1=h%C3%A4h&p2=h%E4h">http://127.0.0.1:3000/?p1=h%C3%A4h&amp;p2=h%E4h</a></blockquote>
  48. which has two times the same parameter ("h&auml;h"), the first (p1) in UTF-8 encoding, and the second (p2) in ISO-8859-1.</p>
  49. <% } %>
  50. </body>
  51. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement