Created
March 31, 2019 02:22
-
-
Save sironekotoro/4bb6ac81ac8fc005cd8a0696e0d5b323 to your computer and use it in GitHub Desktop.
Perl入学式 2018 第5回 Fizzbuzz解き方3パターン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
get '/' => sub { | |
my $c = shift; | |
$c->render( template => 'fizzbuzz' ); | |
}; | |
app->start; | |
__DATA__ | |
@@ fizzbuzz.html.ep | |
% layout 'default'; | |
% title 'Fizzbuzz 解法その1'; | |
<h1><%= title %></h1> | |
<h2><%= b('1行ずつ % , <%= %>で分けて書いた例') %></h2> | |
% for my $num (1..100){ | |
% if ($num % 15 == 0){ | |
<%= 'fizzbuzz' %> <br> | |
% }elsif($num % 3 == 0){ | |
<%= 'fizz' %> <br> | |
% }elsif($num % 5 == 0){ | |
<%= 'buzz' %> <br> | |
% }else{ | |
<%= $num %> <br> | |
% } | |
% } | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
get '/' => sub { | |
my $c = shift; | |
$c->render( template => 'fizzbuzz' ); | |
}; | |
app->start; | |
__DATA__ | |
@@ fizzbuzz.html.ep | |
% layout 'default'; | |
% title 'Fizzbuzz 解法その2'; | |
<h1><%= title %></h1> | |
<h2><%= b('複数行の連続するPerl部をまとめて書いた例') %></h2> | |
<% for my $num (1..100){ | |
if ($num % 15 == 0){ %> | |
'fizzbuzz'<br> | |
<% } | |
elsif($num % 3 == 0) | |
{ %> | |
'fizz'<br> | |
<% } | |
elsif($num % 5 == 0) | |
{ %> | |
'buzz'<br> | |
<% } | |
else | |
{ %> | |
<%= $num %><br> | |
<% } | |
} %> | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
get '/' => sub { | |
my $c = shift; | |
my $fizzbuzz = fizzbuzz(); | |
$c->stash( fizzbuzz => $fizzbuzz ); | |
$c->render( template => 'fizzbuzz' ); | |
}; | |
sub fizzbuzz { | |
my @array = (); | |
for my $num ( 1 .. 100 ) { | |
if ( $num % 15 == 0 ) { | |
push @array, 'fizzbuzz'; | |
} | |
elsif ( $num % 3 == 0 ) { | |
push @array, 'fizz'; | |
} | |
elsif ( $num % 5 == 0 ) { | |
push @array, 'buzz'; | |
} | |
else { | |
push @array, $num; | |
} | |
} | |
my $array_ref = \@array; | |
return $array_ref; | |
} | |
app->start; | |
__DATA__ | |
@@ fizzbuzz.html.ep | |
% layout 'default'; | |
% title 'Fizzbuzz 解法その3'; | |
<h1><%= title %></h1> | |
<h2><%= b('fizzbuzz生成をModelとしてサブルーチンに独立させ、MVCモデルに近づけた例') %></h2> | |
% for my $line ( @{$fizzbuzz} ){ | |
<%= $line %><br> | |
% } | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment