2013年9月4日水曜日

開発環境

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

その他参考書籍

2.

コード(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';

my $dir = '/etc';
chdir $dir or die "can't chdir to $dir: $!";

while (1) {
    chomp(my $pattern = <STDIN>);
    last if $pattern =~ /^\s*$/;
    my @matched  = grep {
        eval{/$pattern/};
    } glob '*';
    if ($@) {
        warn $@;
    } else {
        say join "\n", @matched;
    }
}

入出力結果(Terminal)

$ ./sample.pl
conf$
6to4.conf
asl.conf
autofs.conf
dnsextd.conf
ftpd.conf
gdb.conf
kern_loader.conf
man.conf
memberd.conf
named.conf
newsyslog.conf
notify.conf
ntp-restrict.conf
ntp.conf
pf.conf
resolv.conf
rtadvd.conf
syslog.conf
(
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE / at ./sample.pl line 17, <STDIN> line 2.

$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

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

import glob
import os
import re

dir = '/etc'
os.chdir(dir)

while True:
    pattern = input()
    if re.search(r"^\s*$", pattern):
        break
    try:
        matched = filter(lambda x: re.search(pattern, x), glob.glob('*'))
        print("\n".join(list(matched)))
    except Exception as err:
        print(type(err), err, err.args)

入出力結果(Terminal)

$ ./sample.py
conf$
6to4.conf
asl.conf
autofs.conf
dnsextd.conf
ftpd.conf
gdb.conf
kern_loader.conf
man.conf
memberd.conf
named.conf
newsyslog.conf
notify.conf
ntp-restrict.conf
ntp.conf
pf.conf
resolv.conf
rtadvd.conf
syslog.conf
(
<class 'sre_constants.error'> unbalanced parenthesis ('unbalanced parenthesis',)

$

0 コメント:

コメントを投稿