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

なるほどなるほど。

コンテナを走らせよう

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run centos echo “hello world”
hello world

何!? どういう事? 

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run centos echo "hello world"
hello world

docker psで一覧を見ることができる。
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps

動作が終了したものを見るのは docker ps -a
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b8011965ea0 centos “echo ‘hello world'” 5 minutes ago Exited (0) 5 minutes ago romantic_kapitsa
c6490f99e041 hello-world “/hello” 40 minutes ago Exited (0) 40 minutes ago distracted_wilson
1abf8bdb8845 hello-world “/hello” 40 minutes ago Exited (0) 40 minutes ago sad_colden

vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker run -d centos free -s 3
45e5501bd8ef3b3dd8ec4a717e992550222cb68743d78bcc4dd88db5f3673cba
vagrant@vagrant-ubuntu-trusty-64:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45e5501bd8ef centos “free -s 3” 11 seconds ago Up 10 seconds compassionate_mccarthy

$ sudo docker logs 45e
total used free shared buff/cache available
Mem: 501708 155076 82484 416 264148 325451
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155068 82484 416 264156 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155068 82484 416 264156 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155060 82484 416 264164 325467
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155056 82484 416 264168 325479
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155048 82484 416 264176 325479
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155048 82484 416 264176 325487
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155040 82484 416 264184 325487
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155164 82360 416 264184 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155156 82360 416 264192 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155156 82360 416 264192 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155148 82360 416 264200 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155148 82360 416 264200 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155136 82360 416 264212 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155136 82360 416 264212 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155128 82360 416 264220 325387
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155128 82360 416 264220 325407
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155120 82360 416 264228 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155120 82360 416 264228 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155112 82360 416 264236 325415
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155496 81616 416 264596 325039
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155484 81616 416 264608 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155484 81616 416 264608 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155476 81616 416 264616 325047
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155228 81864 416 264616 325307
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155228 81856 416 264624 325299
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155232 81852 416 264624 325295
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155172 81844 416 264692 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155172 81844 416 264692 325363
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155164 81844 416 264700 325371
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155160 81844 416 264704 325371
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155152 81844 416 264712 325383
Swap: 0 0 0

total used free shared buff/cache available
Mem: 501708 155152 81844 416 264712 325383
Swap: 0 0 0

こんなのやった記憶ねーぞ