2012年8月18日土曜日

開発環境

『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の4章(サブルーチン)、4.12(練習問題)2を解いてみる。

2.

コード(TextWrangler)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDIN, ':encoding(UTF-8)';

sub total{
  my $result = 0;
  for(@_){
    $result += $_;
  }
  $result;
}
print "1 + 2 + ・・・ + 1000 = " . total(1..1000) . "\n";

入出力結果(Terminal)

$ ./sample.pl
1 + 2 + ・・・ + 1000 = 500500
$

0 コメント:

コメントを投稿