2012年3月16日金曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9)の9章(正規表現によるテキスト処理), 9.6(練習問題)6を解いてみる。

ファイル、sample_text

this story is rendered all in lowercase so that you can search
for fred and barney in lowercase, as we usually do. some lines
mention a name like fred more than once so that you can search
for repeated words, for example. (don't actually try to read this
story, which is terribly inane, or you'll be disappointed. we
promise.)



- - - - - - - - - - - - - - - - - - - - - - - - - - - -

one evening, fred and barney were getting ready to go bowling.
wilma saw this and said to betty, "you should ask barney to take
you out to the hard rock cafe for pterodactyl burgers."

betty agreed. "barney and fred never think about us. we should
make it a double date."

"do you want to bring pebbles and bamm-bamm along?"

"sure," said betty. "they always like to play skee-ball."

so wilma found fred and told him, "hey, fred, you've been going
out bowling every night this week. betty and i want to go to the
hard rock cafe for pterodactyl burgers."

"but wilma," said fred, "barney and i need to go tonight. this is
the night that our league is pitted against the gravel pit
strikers. how about if i take you out on monday night?"

that wasn't good enough for wilma. "fred flintstone, if you think
i'm going to let you leave me at home with pebbles while you go
bowling for the tenth night in a row, you've got another think
coming. get cleaned up, because we're going."

-

meanwhile, at the rubbles' house, barney was having the same
discussion with betty. "i can't say no to you betty. but fred
said that if we can beat the gravel pit strikers tonight, we'll
qualify for the final round next month. the grand prize is a
romantic vacation on the rock of gibraltar. i want to win that
for you."

"oh, barney! you are so sweet. of course you can go bowling. do
you really think you'll win?"

"well, this is why fred and i have been practicing all week. but
you can't tell wilma what's going on, because fred made me
promise that he could surprise her."

"what are we going to do for tonight?" betty asked. "wilma wants
us all to go out to the hard rock cafe. what time do you have to
be at the alley?"

barney checked the time. "we aren't scheduled to start until half
past 8."

"that's it, then," said betty. i'll create a distraction at about
8. the alley is just around the corner from the restaurant, so
you and fred can sneak over there."

"i'll call fred and tell him our plan."

-

fred had come up with a plan of his own. with his bowling ball in
hand, he tiptoed towards the door, keeping an eye on the door to
the bathroom to see if wilma was coming. whoops! he tripped over
dino's tail. "arf arf arf" yelped the dogosaurus. "what was
that?" called wilma from the bathroom.

"nothing, nothing, dear," said fred, hurriedly hiding the bowling
ball behind his back. "i think dino just saw a mouse."

"a mouse! well, i should hope you'll catch it, we can't have any
mice around pebbles."

"no problem. i've caught it now," lied fred. "i'll just take it
outside and release it."

as he headed again toward the door, wilma emerged from the
bathroom. "what color is it?" she asked.

"uhm, black?" fred responded, holding the bowling ball behind his
back as he walked backwards towards the door.

"betty saw a black mouse last week. i wonder whether this is a
relative. let me see it."

"no, this one is more of a grey mouse, now that i think of it. no
possible relation. no reason for you to look at the mouse."

"fred flintstone, you let me see that mouse this minute!" cried
wilma.

the telephone rang. "you get that, wilma," said fred. "i'll be
back in a moment." he ducked outside.

wilma answered the phone. "hello?" she asked.

"uhm, yes." said barney on the other end of the line, disguising
his voice. "this is mister, uh, rarnard bubble calling from the
department of, uhm, mineral, uhm, transportation. i'm calling for
mister fred flintstone. is mister fred flintstone available to
come to the telephone?"

at that moment, fred rushed back into the house, having left the
bowling ball in the car. "it's for you, fred," said wilma.
"mister bubble from the department of mineral transportation."

"hello," fred said into the phone, not understanding.

"heya, fred," said barney. "betty and i have figured out what to
do. she'll distract wilma, and you and i can sneak over to the
bowling alley."

"yes, yes," said fred. wilma stood by, curious about this
conversation.

barney continued. "you and wilma go ahead to the restaurant.
after you're gone, betty and i will get your bowling ball and
bring it with us. where do you keep it?"

wilma was getting more curious. fred said, "well, that mineral, i
don't think i have any of it around the house right now."

"but fred," said barney, "it's a bowling ball, not a mineral."

"i'm sure i can't help you with that."

"fred, where do you keep your bowling ball? is it in the garage?"

"no, none in the garage, i'm sure of it."

wilma interrupted him. "fred, give me the phone. i'll take care
of this while you shave."

she pulled the phone away from fred in time to hear barney say,
"no, bamm-bamm, don't play with daddy's bowling ball!"

"barney, is that you?" she asked the phone.

"yep. i mean," said barney, hastily disguising his voice again,
"hello? hello? i think we have a bad connection. i think i just
heard someone named barney on the line. but there is no one named
barney here. no barney at all. put that bowling ball down,
bamm-bamm!"

end of story

2.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

my $in = $ARGV[0];
my $out = $in;
$out =~ s/(\s.\w+)?$/.out/;
open IN, "<$in";
open OUT, ">$out";
while(<IN>){
  s/Fred/Larray/gi;
  print OUT $_;
}

入出力結果(Terminal)

$ ./perl_program sample_text
$ cat sample_text.out
this story is rendered all in lowercase so that you can search
for Larry and barney in lowercase, as we usually do. some lines
mention a name like Larry more than once so that you can search
for repeated words, for example. (don't actually try to read this
story, which is terribly inane, or you'll be disappointed. we
promise.)



- - - - - - - - - - - - - - - - - - - - - - - - - - - -

one evening, Larry and barney were getting ready to go bowling.
wilma saw this and said to betty, "you should ask barney to take
you out to the hard rock cafe for pterodactyl burgers."

betty agreed. "barney and Larry never think about us. we should
make it a double date."

"do you want to bring pebbles and bamm-bamm along?"

"sure," said betty. "they always like to play skee-ball."

so wilma found Larry and told him, "hey, Larry, you've been going
out bowling every night this week. betty and i want to go to the
hard rock cafe for pterodactyl burgers."

"but wilma," said Larry, "barney and i need to go tonight. this is
the night that our league is pitted against the gravel pit
strikers. how about if i take you out on monday night?"

that wasn't good enough for wilma. "Larry flintstone, if you think
i'm going to let you leave me at home with pebbles while you go
bowling for the tenth night in a row, you've got another think
coming. get cleaned up, because we're going."

-

meanwhile, at the rubbles' house, barney was having the same
discussion with betty. "i can't say no to you betty. but Larry
said that if we can beat the gravel pit strikers tonight, we'll
qualify for the final round next month. the grand prize is a
romantic vacation on the rock of gibraltar. i want to win that
for you."

"oh, barney! you are so sweet. of course you can go bowling. do
you really think you'll win?"

"well, this is why Larry and i have been practicing all week. but
you can't tell wilma what's going on, because Larry made me
promise that he could surprise her."

"what are we going to do for tonight?" betty asked. "wilma wants
us all to go out to the hard rock cafe. what time do you have to
be at the alley?"

barney checked the time. "we aren't scheduled to start until half
past 8."

"that's it, then," said betty. i'll create a distraction at about
8. the alley is just around the corner from the restaurant, so
you and Larry can sneak over there."

"i'll call Larry and tell him our plan."

-

Larry had come up with a plan of his own. with his bowling ball in
hand, he tiptoed towards the door, keeping an eye on the door to
the bathroom to see if wilma was coming. whoops! he tripped over
dino's tail. "arf arf arf" yelped the dogosaurus. "what was
that?" called wilma from the bathroom.

"nothing, nothing, dear," said Larry, hurriedly hiding the bowling
ball behind his back. "i think dino just saw a mouse."

"a mouse! well, i should hope you'll catch it, we can't have any
mice around pebbles."

"no problem. i've caught it now," lied Larry. "i'll just take it
outside and release it."

as he headed again toward the door, wilma emerged from the
bathroom. "what color is it?" she asked.

"uhm, black?" Larry responded, holding the bowling ball behind his
back as he walked backwards towards the door.

"betty saw a black mouse last week. i wonder whether this is a
relative. let me see it."

"no, this one is more of a grey mouse, now that i think of it. no
possible relation. no reason for you to look at the mouse."

"Larry flintstone, you let me see that mouse this minute!" cried
wilma.

the telephone rang. "you get that, wilma," said Larry. "i'll be
back in a moment." he ducked outside.

wilma answered the phone. "hello?" she asked.

"uhm, yes." said barney on the other end of the line, disguising
his voice. "this is mister, uh, rarnard bubble calling from the
department of, uhm, mineral, uhm, transportation. i'm calling for
mister Larry flintstone. is mister Larry flintstone available to
come to the telephone?"

at that moment, Larry rushed back into the house, having left the
bowling ball in the car. "it's for you, Larry," said wilma.
"mister bubble from the department of mineral transportation."

"hello," Larry said into the phone, not understanding.

"heya, Larry," said barney. "betty and i have figured out what to
do. she'll distract wilma, and you and i can sneak over to the
bowling alley."

"yes, yes," said Larry. wilma stood by, curious about this
conversation.

barney continued. "you and wilma go ahead to the restaurant.
after you're gone, betty and i will get your bowling ball and
bring it with us. where do you keep it?"

wilma was getting more curious. Larry said, "well, that mineral, i
don't think i have any of it around the house right now."

"but Larry," said barney, "it's a bowling ball, not a mineral."

"i'm sure i can't help you with that."

"Larry, where do you keep your bowling ball? is it in the garage?"

"no, none in the garage, i'm sure of it."

wilma interrupted him. "Larry, give me the phone. i'll take care
of this while you shave."

she pulled the phone away from Larry in time to hear barney say,
"no, bamm-bamm, don't play with daddy's bowling ball!"

"barney, is that you?" she asked the phone.

"yep. i mean," said barney, hastily disguising his voice again,
"hello? hello? i think we have a bad connection. i think i just
heard someone named barney on the line. but there is no one named
barney here. no barney at all. put that bowling ball down,
bamm-bamm!"

end of story
$

1 コメント :

Unknownさんのコメント...

For certain i will always point out this sit back and watch creates among the better hobby timepieces round might can quickly communicate little or no related to all of them. Going back a long time together with extraordinary persistence http://www.replicabestsale.co.uk, the actual check out comes with presented two or three completely new pursuit timepieces offering an ideal trend, useful plus handy intricate activities, manly design, in addition to truthful costs. The year of 2010 which usually get worse involving qualities realises themselves during the confined release clock. It all sounds like a wristwatch seems to have developed well before so it applies a pre-existing movements and similar company name seeing that different devices. What we can be viewing is mostly a improvement in relation to dial layout in addition to total speech contained in the much larger plus undoubtedly well liked variety of activity articles. Just what exactly remains a collection along with the exception of massive dimensions not to mention military-inspired appearances will be top. The software keeps going in a different way from screw-down capped teeth inside that you just twist it again in area to an alternative. White signs methods that must be locked despite the fact that reddish symptoms usually means to be offered just for rotating or simply appearing drawn out there.

コメントを投稿