-
Notifications
You must be signed in to change notification settings - Fork 5
/
tryperl.pl
executable file
·118 lines (94 loc) · 2.92 KB
/
tryperl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/perl
# Project : TryPerl
# Author : Thibault Duponchelle
# Main script for TryPerl website
use strict;
use warnings;
use Mojolicious::Lite;
use lib "lessons";
@ARGV = qw( daemon ) unless scalar @ARGV;
websocket '/executor/:num' => sub {
my $self = shift;
warn "Client connected to executor\n";
$self->on(finish => sub {
warn "Client disconnected from executor\n";
} );
$self->on(message => sub {
my ( $self, $message ) = @_;
($message eq "ping") and return;
require executor;
my $num = $self->stash('num');
eval "require lesson$num";
if($@) {
$self->send( { text => "Not a lesson" });
} else {
my $result = execute_this($message);
my $answer = answer($message);
$self->send( { text => "$result"."$answer" });
}
});
};
websocket '/lesson' => sub {
my $self = shift;
warn "Client connected to lesson\n";
$self->on( finish => sub {
warn "Client disconnected from lesson\n";
} );
$self->on(message => sub {
my ( $self, $message ) = @_;
($message eq "ping") and return;
delete $INC{"lesson$message.pm"}; # Force module reload. FIXME: waste IO
eval "require lesson$message";
if($@) {
$self->send( { text => "Not a lesson" });
} else {
my @lines = <DATA>;
my $str = join "\n", @lines;
$self->send( { text => "$str" });
}
});
};
get '/:n' => 'index';
get '/' => sub {
my $self = shift;
$self->redirect_to('/0');
};
app->start;
1;
__DATA__
@@ index.html.ep
% my $url = $self->req->url->to_abs->scheme( $self->req->is_secure ? 'wss' : 'ws' )->path( '/' );
% use config;
<html>
<head>
<title>Try Perl: learn the basics of the Perl language in your browser</title>
<meta name="Content-Type" content="text/html; charset=UTF-8">
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="pixs/faviconion.ico" type="image/x-icon">
<link rel="icon" href="pixs/faviconion.ico" type="image/x-icon">
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery.console.js"></script>
<script type="text/javascript" src="js/tryperl.js"></script>
<script type="text/javascript">
$(document).ready(function(){
initTryPerlClient("<%= $url %>", <%= get_max_lessons() %>, <%= $n %>);
});
</script>
</head>
<body>
<div id="left-block">
<img class="onion" src="pixs/onion-big.svg"/>
<div id="left-block-content">
<div id="lesson">
</div>
</div>
</div>
<div id="middle-block">
</div>
<div id="right-block">
</div>
<div id="left-footer"><a href="https://github.com/thibaultduponchelle/tryperl">Source Code</a> - <a href="https://github.com/chrisdone/jquery-console">console</a> - <a href="https://try.ruby-lang.org/">tryruby</a></div>
<div id="right-footer"><code>clear</code> clear editor - <code>back</code> previous lesson - <code>next</code> next lesson</div>
<script type="text/javascript" src="js/ga.js"></script>
</body>
</html>