-
Notifications
You must be signed in to change notification settings - Fork 1
/
gobble-simple.pl
executable file
·87 lines (66 loc) · 2.22 KB
/
gobble-simple.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
#!/usr/bin/env perl
use Mojolicious::Lite;
# Grab the command line variables
# by accessing the @ARGV array
my $my_url = $ARGV[0]; # The first is the url parameter
my $my_command = $ARGV[1]; # The second is the command
# We need two threads. One for screen and the other
# for Mojolicious and our web page.
my $childpid = fork();
if ($childpid) { # If this is the child thread
# Send the command to the shell as a parameter of screen
`screen -L $my_command`;
# I want mojo to continue running after the child
# is done.. So just give a little reminder to kill it.
print "Done...\n";
print "Don't forget to kill the process 'kill -9 $childpid'\n";
} else { # If not this is the parent thread
get $my_url => sub { # Set the url to $my_url
my $self = shift; # Grab the current instance
# Open the log file and store it in <FILE>
open(FILE,"html_gobble.log");
# Create an empty array that will hold all our content
my @new_file = ();
# In web land you need a <br> tag to end a line
# or encase the line in <div> tags. Let's do the later.
# There are tag generating tools in Mojolicious.. But let's
# Do it the old fashioned way.
#
# Use perl's "magic open" to open and run through
# each line of the file.
while (<FILE>) {
# The $_ variable holds the current line
chomp $_; # Remove the newline character
$_ =~ s/^/<div>/; # add <div> to the beginning
$_ =~ s/$/<\/div>/; # add </div> to the end
push(@new_file, $_); # add the line to our array
}
# Flatten the array into one long string with no spaces or
# newline characters.
my $one_string = join('',@new_file);
# Tell Mojo to render the index template below
# and pass our data "$one_string" as a new
# variable called "$log_data"
$self->render('index', log_data => $one_string);
};
# Start Mojolicious with the light weight web server
app->start('daemon');
}
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Test';
<%== $log_data %>
@@ layouts/default.html.ep
<!doctype html><html>
<head>
<title><%= title %></title>
%# This tells phone browsers not to scale out
%# when first loaded
<meta name="viewport" content="initial-scale=1.0"/>
<%= base_tag %>
</head>
<body>
<%= content %>
</body>
</html>