2013年9月14日土曜日

開発環境

『続・初めての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 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
use IO::Dir;

for (@ARGV) {
    my $dir_fh = IO::Dir->new( $_ ) || die "Could not open dirhandle: $!";
    say $_;
    while (defined( my $file = $dir_fh->read)) {
        say "    $file";
    }
}

入出力結果(Terminal)

$ ./sample.pl . ../python
.
    .
    ..
    .DS_Store
    .out
    __pycache__
    barney
    betty
    coconet.dat
    coconet_total.dat
    coconet_total_2.dat
    date.txt
    distribute-0.6.34.tar.gz
    fred
    Gilligan:
    Ginger:
    hello_world.pl
    html
    link_test
    ln.txt
    ln1.txt
    log
    log.dat
    log_file.txt
    Lovey:
    ls.out
    MaryAnn:
    MonkeyMan:
    numbers
    Oogaboogoo
    perl_kamimura_blog
    perl_kamimura_blog.html
    perl_program1
    Professor:
    result
    sample
    sample.pl
    sample.py
    sample.txt
    sample_folder
    sample_text
    Skipper:
    some_file
    some_folder
    sortable_hash
    standings.db
    test.out
    test.py
    test.txt
    test.txt.out
    test_folder
    test_link
    Thurston:
    tmp.txt
    tmp1.txt
    tmp2.txt
    total_bytes.dat
    untitled text 2.txt
../python
    .
    ..
     *Minibuf-2*
    # *Minibuf-2*#
    #sample.py#
    #y#
    .DS_Store
    __pycache__
    build
    c_program
    c_program.c
    c_program.dSYM
    changer.py
    file.py
    html
    kamimura.jpg
    myclient.py
    myclient1.py
    myclient2.py
    myfile.txt
    mypkg
    person_name.xml
    personal_name.txt
    python_kamimura_blog
    python_kamimura_blog.html
    python_program.pyc
    python_program1.py
    recur1.py
    recur2.py
    sample.py
    sample.pyc
    sample.py~
    sample.txt
    sample_dir
    sample_folder
    some_file
    tmp.pl
    tmp.py
    tmp.txt
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os
import sys
import glob

for dir in sys.argv[1:]:
    path = os.path.abspath(dir)
    print(dir)
    for file in glob.glob('{0}{1}*'.format(path, os.path.sep)):
        print('    {0}'.format(os.path.basename(file)))

入出力結果(Terminal)

$ ./sample.py . ../python
.
    __pycache__
    barney
    betty
    coconet.dat
    coconet_total.dat
    coconet_total_2.dat
    date.txt
    distribute-0.6.34.tar.gz
    fred
    Gilligan:
    Ginger:
    hello_world.pl
    html
    link_test
    ln.txt
    ln1.txt
    log
    log.dat
    log_file.txt
    Lovey:
    ls.out
    MaryAnn:
    MonkeyMan:
    numbers
    Oogaboogoo
    perl_kamimura_blog
    perl_kamimura_blog.html
    perl_program1
    Professor:
    result
    sample
    sample.pl
    sample.py
    sample.txt
    sample_folder
    sample_text
    Skipper:
    some_file
    some_folder
    sortable_hash
    standings.db
    test.out
    test.py
    test.txt
    test.txt.out
    test_folder
    test_link
    Thurston:
    tmp.txt
    tmp1.txt
    tmp2.txt
    total_bytes.dat
    untitled text 2.txt
../python
     *Minibuf-2*
    # *Minibuf-2*#
    #sample.py#
    #y#
    __pycache__
    build
    c_program
    c_program.c
    c_program.dSYM
    changer.py
    file.py
    html
    kamimura.jpg
    myclient.py
    myclient1.py
    myclient2.py
    myfile.txt
    mypkg
    person_name.xml
    personal_name.txt
    python_kamimura_blog
    python_kamimura_blog.html
    python_program.pyc
    python_program1.py
    recur1.py
    recur2.py
    sample.py
    sample.pyc
    sample.py~
    sample.txt
    sample_dir
    sample_folder
    some_file
    tmp.pl
    tmp.py
    tmp.txt
$

0 コメント:

コメントを投稿