Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Perl入学式 2018 第5回 Fizzbuzz解き方3パターン
#!/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行ずつ % , &lt;%= %&gt;で分けて書いた例') %></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>
#!/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>
#!/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
You can’t perform that action at this time.