2019年3月10日日曜日

開発環境

初めてのPerl 第7版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)近藤 嘉雪(翻訳)嶋田 健志(翻訳)オライリージャパン)の4章(サブルーチン)、4.13(練習問題)1の解答を求めてみる。

コード

#!/usr/bin/env perl
use strict;
use utf8;
use v5.18;
# Wide character in print at ... line ...が出力されないようにに指定
# 付録C(Unicode入門)、C.4(ファンシーな文字)、C.4.2(さらにファンシーな文字)より
# WindowsとmacOSのどちらでも標準入力、標準出力、標準エラーについてちゃんと動くように設定
use Encode::Locale;
binmode STDIN, ':encoding(console_in)';
binmode STDOUT, ':encoding(console_out)';
binmode STDERR, ':encoding(console_out)';

say '1.';
my @fred = qw{1 3 5 7 9};
my $fred_total = total(@fred);
say "The total of \@fred is $fred_total.";

say "Enter some numbers on separate lines: ";
my $user_total = total(<STDIN>);
say "The total of those numbers is $user_total.";

sub total {
    my $total = 0;
    for (@_) {
        $total += $_;
    }
    $total;
}

入出力結果(cmd(コマンドプロンプト)、Terminal)

C:\Users\...>type input1.txt
2
4
6
8
10

C:\Users\...>perl sample1.pl < input1.txt
1.
The total of @fred is 25.
Enter some numbers on separate lines: 
The total of those numbers is 30.

C:\Users\...>

0 コメント:

コメントを投稿