perlで正規表現

open(my $in, "<", "test.dat") or die("could not open file.");

while(<$in>){
	$_ =~ s/abc/ABC/;
	print $_;
}

close($in);

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
ABC
def
ghi
123
456

while(<$in>){
	$_ =~ s/abc/ABC/;
	if($_ =~ /[a-z]/){
		print $_;
	}	
}

なるほど、面白い
vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
def
ghi

サブルーチンをつくる。

sub max {
	my $max = $_[0];
	if ($_[1] > $max){
		$max = $_[1];
	}
	return $max;
}
print max(2, 8);

サブルーチンという名称ですが、要するに関数ですな。

sub max {
	my $max = shift(@_);
	foreach(@_){
		$max = $_ if $_ > $max;
	}
	return $max;
}
print max(2, 8, 100, 23, 38);

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
100

なんかかなり駆け足できましたが、基礎はこれ位にしてdancerに入りましょう。

perlでファイルの読み書きをしよう

fileをopenします。

open(my $in, "<", "test.dat") or die("could not open file.");

while(<$in>){
	print $_;
}

close($in);

vagrant@vagrant-ubuntu-trusty-64:~/app$ cat test.dat
abc
def
ghi
123
456
789vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
abc
def
ghi
123
456
789

open(my $in, "<", "test.dat") or die("could not open file.");
open(my $out, ">", "test.out") or die("could not open file.");

while(<$in>){
	print $out $_;
}

close($in);
close($out);

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
vagrant@vagrant-ubuntu-trusty-64:~/app$ cat test.out
abc
def
ghi
123
456
789

書き込みですな。

ガンガンperlをやろう

# ループ処理
my $i = 0;
while($i < 10){
	print "i = $i\n";
	$i++;
}
&#91;/perl&#93;

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

for文
&#91;perl&#93;
for(my $i = 0; $i < 10; $i++){
	print "i = $i\n";
}
&#91;/perl&#93;

おおおおおお、foreachが可笑しなことになってる。。
&#91;perl&#93;
my @colors = qw(red green blue orange pink);

foreach my $color (@colors){
	print "color = $color\n";
}
&#91;/perl&#93;

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
color = red
color = green
color = blue
color = orange
color = pink

ナニコレ?
&#91;perl&#93;
my @colors = qw(red green blue orange pink);

foreach (@colors){
	print "color = $_\n";
}
&#91;/perl&#93;

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
color = red
color = green
color = blue
color = orange
color = pink

これは。。。
&#91;perl&#93;
my %sales = ("tanaka"=>150, "suzuki"=>300, "ohira"=>200);

foreach my $key (keys(%sales)){
		print "sales for $key in $sales{$key}\n"
}

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
sales for tanaka in 150
sales for suzuki in 300
sales for ohira in 200

Perlを思い出そう2

my @sales = (300, 150, 200);
# @sales $#sale: 末尾の添字

print @sales[0..$#sales];

key and value

my %sales = ("tanaka"=>300, "suzuki"=>150, "ohira"=>200);

print $sales{"tanaka"};

条件分岐

my $score = 85;

if ($score >= 80) {
	print("ok!");
}

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
ok!

my $score = 85;
my $name = "Luke";

if ($name eq "Luke") {
	print("I am your father!");
}

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
I am your father!

ああああああ、何やってんだろう。

my $score = 85;
my $name = "Luke";

if (($name eq "Luke") &&($score >= 80)) {
	print("I am your father!");
}

Perlを思い出そう

my $x = 10;
my $y = 10.75;
my $z = 12_2425_241;

my $xx = 10 * 10;
my $yy = 10 % 3;
my $zz = 2 ** 3;

$x++;
$x--;

$x += 5;
my $a = "he\tllo"; # 特殊文字(\n, \t)が使える、変数展開がされる
my $b = 'he\tllo';

print $a;
print $b;

hellohellovagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
he llohe\tllo

こうも書ける。

my $name = "tom";
my $a = "hello, $name\n"; # 特殊文字(\n, \t)が使える、変数展開がされる
my $b = 'hello';

print $a;
print $b;

# . x 文字の連結

配列変数

my @sales = (150, 200, 300);
my @colors = ('red', 'green');
my @mixed = (150, 'red', 1.5);

print @sales;

my @inc = (1..100);

print @inc;

配列の読み出し

my @sales = (300, 150, 200);

print $sales[1];
my @sales = (300, 150, 200);

print @sales[0..2];

perl hello.pl

use strict;
use warnings;

print("hello world\n");

vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
hello world

my $msg;
$msg = "Hello World\n";

print $msg;

こうも書ける。

my $msg = "Studying Perl\n";
print $msg;

Docker Hubにsign upしよう

あれ、昔アカウントを作っていたみたいだ。全く覚えていない。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker push hpscript/httpd
The push refers to repository [docker.io/hpscript/httpd]
a722e4d2fe46: Preparing
fa760d3a1093: Preparing
1d31b5806ba4: Preparing
denied: requested access to the resource is denied

ん?
docker tag firstimage YOUR_DOCKERHUB_NAME/firstimage

なるほど。リポジトリ名がddddockerでないと駄目なのね。
あ~ 頭痛くなってきた。

Dockerfileを使おう

vi Dockerfile

From centos
MAINTAINER Tetsu <hogehoge@gmail.com>
# Run: execute in build
RUN echo "now building..."
# CMD: execute in run
CMD ["echo", "now running..."]

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker build -t shibuya/echo .
Sending build context to Docker daemon 13.31kB
Step 1/4 : From centos
—> 5182e96772bf
Step 2/4 : MAINTAINER Tetsu
—> Running in 48a636f08d6c
Removing intermediate container 48a636f08d6c
—> a532962fac42
Step 3/4 : RUN echo “now building…”
—> Running in 266d3e002a78
now building…
Removing intermediate container 266d3e002a78
—> 42e0d4397d0a
Step 4/4 : CMD [“echo”, “now running…”]
—> Running in 66419b3311b9
Removing intermediate container 66419b3311b9
—> b503e170fb73
Successfully built b503e170fb73
Successfully tagged shibuya/echo:latest

なんじゃこりゃーーーーーーーー

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
shibuya/echo latest b503e170fb73 46 seconds ago 200MB
ab93f4f1679d 15 minutes ago 200MB
hello-world latest 4ab4c602aa5e 2 days ago 1.84kB
centos latest 5182e96772bf 4 weeks ago 200MB
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run b503
now running…

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker build -t hpscript/httpd .
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -p 8080:80 -d hpscript/httpd
32e7d390b05826510945df2468a3e98a5762586080ec508ac3543a068ec3390f

わかったような解らんような分からんw

コンテナ内に入って作業しよう

コンテナ内のcentosに入ります。

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -i -t centos /bin/bash
[root@83a948642dbe /]# touch hello.text
[root@83a948642dbe /]# ls
anaconda-post.log dev hello.text lib media opt root sbin sys usr
bin etc home lib64 mnt proc run srv tmp var
[root@83a948642dbe /]# exit
exit

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
83a948642dbe centos “/bin/bash” 2 minutes ago Exited (0) About a minute ago elated_swartz
45e5501bd8ef centos “free -s 3” 10 minutes ago Up 10 minutes compassionate_mccarthy
0b8011965ea0 centos “echo ‘hello world'” 19 minutes ago Exited (0) 19 minutes ago romantic_kapitsa
c6490f99e041 hello-world “/hello” About an hour ago Exited (0) About an hour ago distracted_wilson
1abf8bdb8845 hello-world “/hello” About an hour ago Exited (0) About an hour ago sad_colden

sudo docker commitでcontainerからimageを作る。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker commit 83a
sha256:ab93f4f1679d8efa1843606ea3d8741b35fe3f9b73e1ddb8acbfbd2ddf25f468
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ab93f4f1679d 35 seconds ago 200MB
hello-world latest 4ab4c602aa5e 2 days ago 1.84kB
centos latest 5182e96772bf 4 weeks ago 200MB
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -i -t ab9 /bin/bash
[root@5e92b8233188 /]# ls
anaconda-post.log dev hello.text lib media opt root sbin sys usr
bin etc home lib64 mnt proc run srv tmp var

なるほどなるほど。