開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の9章(リファレンスを使った実践的なテクニック)の9.9(練習問題)4を解いてみる。
その他参考書籍
4.
コード(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 File::Basename;
sub data_for_path {
my $path = shift;
if (-f $path or -l $path) {
return undef;
}
if (-d $path) {
my %directory;
opendir PATH, $path or die "Cannot opendir $path: $1";
my @names = readdir PATH;
closedir PATH;
for my $name (@names) {
next if $name eq '.' or $name eq '..';
$directory{$name} = data_for_path("$path/$name");
}
return \%directory;
}
warn "$path is neither a file nor a directory\n";
return undef;
}
sub dump_data_for_path {
my $pre = shift;
my $path = shift;
my $data = shift;
print " " x $pre . $path;
if (not defined $data) {
print "\n";
return;
}
my %directory = %$data;
if (%directory) {
print ", with contents:\n";
for (sort keys %directory) {
$pre += 1;
dump_data_for_path($pre, $_, $directory{$_});
$pre -= 1;
}
} else {
print ", an empty directory\n";
}
}
dump_data_for_path(0, ".", data_for_path('.'));
入出力結果(Terminal)
$ ./sample.pl
., with contents:
.DS_Store
.out
Gilligan:
Ginger:
Lovey:
MaryAnn:
MonkeyMan:
Oogaboogoo, with contents:
__pycache__, with contents:
date.cpython-33.pyc
date.pm
date.py
Professor:
Skipper:
Thurston:
__pycache__, with contents:
sample.cpython-32.pyc
sample.cpython-33.pyc
barney
betty
coconet.dat
coconet_total.dat
coconet_total_2.dat
date.txt
distribute-0.6.34.tar.gz
empty_folder, an empty directory
fred
hello_world.pl
html
link_test
ln.txt
ln1.txt
log
log.dat
log_file.txt
ls.out
numbers
perl_kamimura_blog
perl_kamimura_blog.html
perl_program1
result
sample, with contents:
.DS_Store
sample_folder, an empty directory
sample_folder copy, an empty directory
sample_folder copy 2, an empty directory
sample_folder copy 3, an empty directory
sample_folder copy 4, an empty directory
sample.pl
sample.py
sample.txt
sample_folder, with contents:
.DS_Store
sample1.bak
test
sample_text
some_file
some_folder, with contents:
.DS_Store
ln.txt
mv1.txt
sample.pl
some_file
some_file1
some_file2
symlink.txt
sortable_hash
standings.db
test.out
test.py
test.txt
test.txt.out
test_folder, with contents:
.DS_Store
sample, with contents:
sample.txt
test_link
tmp.txt
tmp1.txt
tmp2.txt
total_bytes.dat
untitled text 2.txt
$
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
import os
import glob
def dataForPath(path):
if os.path.isfile(path) or os.path.islink(path):
return
if os.path.isdir(path):
directory = {}
names = glob.glob('{0}{1}*'.format(path, os.path.sep))
for name in names:
directory[name] = dataForPath(path + os.path.sep + name)
return directory
def dumpDataForPath(pre, path, data):
print(" " * pre + os.path.basename(path), end="")
if data == None:
print()
return
if data:
print(", with contents:")
for key in sorted(data.keys()):
pre += 1
dumpDataForPath(pre, key, data[key])
pre -= 1
else:
print(", an empty directory")
dumpDataForPath(0, '.', dataForPath('.'))
入出力結果(Terminal)
$ ./sample.py
., with contents:
Gilligan:
Ginger:
Lovey:
MaryAnn:
MonkeyMan:
Oogaboogoo, with contents:
__pycache__
date.pm
date.py
Professor:
Skipper:
Thurston:
__pycache__, with contents:
sample.cpython-32.pyc
sample.cpython-33.pyc
barney
betty
coconet.dat
coconet_total.dat
coconet_total_2.dat
date.txt
distribute-0.6.34.tar.gz
empty_folder, an empty directory
fred
hello_world.pl
html
link_test
ln.txt
ln1.txt
log
log.dat
log_file.txt
ls.out
numbers
perl_kamimura_blog
perl_kamimura_blog.html
perl_program1
result
sample, with contents:
sample_folder
sample_folder copy
sample_folder copy 2
sample_folder copy 3
sample_folder copy 4
sample.pl
sample.py
sample.txt
sample_folder, with contents:
sample1.bak
test
sample_text
some_file
some_folder, with contents:
ln.txt
mv1.txt
sample.pl
some_file
some_file1
some_file2
symlink.txt
sortable_hash
standings.db
test.out
test.py
test.txt
test.txt.out
test_folder, with contents:
sample
test_link
tmp.txt
tmp1.txt
tmp2.txt
total_bytes.dat
untitled text 2.txt
$
0 コメント:
コメントを投稿