2013年2月1日金曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の8章(ファイルハンドルへのリファレンス), 8.6(練習問題)3を解いてみる。

その他参考書籍

3.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
use IO::Dir;

my $dir_fh;
for (@ARGV) {
    if (-d $_) {
        $dir_fh = IO::Dir->new($_) || die $!;
        while (defined( my $file = $dir_fh->read )) {
            print "$file\n";
        }
    }
}

入出力結果(Terminal)

$ ./sample.pl sample_folder
.
..
.DS_Store
sample1.bak
test
$ ls -a sample_folder
.  ..  .DS_Store sample1.bak test
$

pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import os, sys, glob

for x in sys.argv:
    if os.path.isdir(x):
        for y in map(lambda z: z.split(os.path.sep)[-1], 
                glob.glob(os.path.join(x, "*"))):
            print(y)

入出力結果(Terminal)

$ ./sample.py sample_folder
sample1.bak
test
$

0 コメント:

コメントを投稿