2012年6月14日木曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の10章(さまざまな制御構造), 10.11(練習問題)2を解いてみる。

2.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;

my $Debug = $ENV{DEBUG} // 1;
my $secret = int(1 + rand 100);
print "secret: $secret\n" if $Debug;
print "1から100までの秘密の数当て\n";
while(1){
  chomp(my $num = );
  last if $num =~ /^(\s*|quit|exit)$/;
  if($num == $secret){
    print "secret\n";
    last;
  } elsif ($num > $secret){
    print "Too high\n";
  }else{
    print "Too low\n";
  }
}

入出力結果(Terminal)

$ ./sample.pl
secret: 23
1から100までの秘密の数当て
23
secret
$

0 コメント:

コメントを投稿