2011-01-11
■ [サンプルスクリプト]MySQLのバックアップスクリプトを書いてみた
#!/usr/bin/perl use strict; use warnings; use File::Path; # # Define # my $owner = "user1"; chop(my $user = `whoami`); my $tmp_dir = "/home/$owner/tmp.mysql_backup.$$/"; my $mysqldump = "/home/mysql/bin/mysqldump"; chop(my $current_time = `date +%Y%m%d.%H%M%S`); my $dump_file_name = "dump.sql"; my $tgz_file_name = "mysql." . $current_time . ".tgz"; my $tgz_dir = "/tmp/"; my $tgz_file_path = "${tgz_dir}${tgz_file_name}"; # # Check # if ($owner ne $user) { die("You aren't the user $owner. If you want to change the user, you should edit this script."); } if (-e $tmp_dir) { die("The directory $tmp_dir is already exists. Perhaps, this script has a problem."); } if (! -e $tgz_dir) { die("The directory $tgz_dir is not exists. You should make the directory."); } # # Init # if (!mkdir($tmp_dir)) { die("The function mkdir is failed. The directory is $tmp_dir."); } # # Do mysqldump # my $cmd = "$mysqldump -u mysql -pXXX -A -F --opt --quick --default-character-set=binary > ${tmp_dir}${dump_file_name}"; print "$cmd\n"; `$cmd`; # # Do compress # $cmd = "tar czvpf $tgz_file_path -C $tmp_dir $dump_file_name"; print "$cmd\n"; `$cmd`; # # Terminate # print "Remove the directory $tmp_dir\n"; my $num = rmtree($tmp_dir);
2009-06-05
■ [基礎][文字列]length関数で文字列の長さを求める
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "abc"; print "文字列長:", length($string), "文字\n";
・実行結果
~$ ./hoge.pl 文字列長:3文字
日本語を使うとこうなる
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "あいう"; print "文字列長:", length($string), "文字\n";
・実行結果
~$ ./hoge.pl 文字列長:6文字
6/10追記:dankogai氏からのご指摘によれば、以下の方法はよくないそうです。
(ちなみに、この方法は独習Perl第二版という本に記載されていたものです。。)
日本語の文字数を正確に求めたい場合、use encodingを指定する
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl use encoding "euc-jp"; $string = "あいう"; print "文字列長:", length($string), "文字\n";
※ Windows環境だとたぶん、Shift_JISを指定すると思う。
・実行結果
~$ ./hoge.pl 文字列長:3文字
2009-06-01
■ [基礎][文字列]chomp関数
chomp関数は、改行のみを削除する。
つまり、最後の文字が改行でない場合、何もならない。
返却値として、取り除いた個数をかえすらしい
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "abc\n"; chomp($string); print $string;
・実行結果
~$ ./hoge.pl abc~$
■ [基礎][文字列]chop関数
chop関数は最後の文字(改行ふくむ)を削除する。
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "abc"; chop($string); print $string, "\n";
・実行結果
~$ ./hoge.pl ab
最後の文字が改行の場合、どうなるか?
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "abc\n"; chop($string); print $string;
・実行結果
~$ ./hoge.pl abc~$
■ [基礎][文字列]join関数
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl @arr = ("Tokyo", "Yokohama", "Oosaka"); $string = join(":", @arr); print $string, "\n";
・実行結果
~$ ./hoge.pl Tokyo:Yokohama:Oosaka
■ [基礎][文字列]split関数
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $input = <STDIN>; chomp($input); @arr = split(/\s+/, $input); foreach (@arr) { print "要素: ", $_, "\n"; }
・実行結果
~$ ./hoge.pl This is a pen. 要素: This 要素: is 要素: a 要素: pen.
・サンプルコード(その2)
~$ cat hoge.pl #!/usr/local/bin/perl $input = <STDIN>; chomp($input); @arr = split(/\s+/, $input, 2); foreach (@arr) { print "要素: ", $_, "\n"; }
※ split関数の第3パラメタに2を指定
・実行結果(その2)
~$ ./hoge.pl This is a pen. 要素: This 要素: is a pen.
2009-05-31
■ [基礎][文字列]sprintf関数
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $msg = sprintf("これは%s関数です\n", "sprintf"); print $msg;
・実行結果
~$ ./hoge.pl これはsprintf関数です
■ [基礎][文字列]printf関数
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl printf("これは%s関数です\n", "printf");
・実行結果
~$ ./hoge.pl これはprintf関数です
■ [基礎][ファイル]
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $_ = "こんにちは"; open(F, ">hoge.dat"); print F; close(F);
※ "print F"のところでは、$_の内容をファイルポインタFに書き込んでいる
・実行結果
~$ ./hoge.pl ~$ cat hoge.dat こんにちは~$
■ [基礎][正規表現]計算はe演算子
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "23 45"; $string =~ s/(\d+) (\d+)/$1 + $2/e; print $string;
・実行結果
~$ ./hoge.pl 68
■ [基礎][正規表現]文字列の置き換えはs演算子
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $msg = <<EOT; HIs name is Mike. Mike is student. Mike live in UK. EOT $msg =~ s/Mike/Nancy/g; print $msg, "\n";
・実行結果
~$ ./hoge.pl HIs name is Nancy. Nancy is student. Nancy live in UK.
■ [基礎][正規表現]最短一致
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $html =<<EOT; <html> <head> </head> <body> <font color=red>hello</font><b>goobdy</b> </body> </html> EOT while($html =~ /<.+?>/g) { print $&, "\n"; }
・実行結果
~$ ./hoge.pl <html> <head> </head> <body> <font color=red> </font> <b> </b> </body> </html>
■ [基礎][正規表現]最長一致
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $html =<<EOT; <html> <head> </head> <body> <font color=red>hello</font><b>goobdy</b> </body> </html> EOT while($html =~ /<.+>/g) { print $&, "\n"; } ~$
・実行結果
~$ ./hoge.pl <html> <head> </head> <body> <font color=red>hello</font><b>goobdy</b> </body> </html>
2009-05-26
■ [基礎][正規表現]日本語を使う
独習Perlによれば、perlで日本語を使う場合は、以下の命令が必要らしい。
use encoding "euc-jp";
でも、これを指定しなくても正規表現が使えてるのは謎。。。
(6/6追記)
use-encodingの使いどころがわかった。詳細は以下を参照
■ [応用][正規表現]グループ化の( )について
( )には二つの役割がある。
一つはグループ化。
もう一つは参照(文字列の範囲を$1とか$2で参照するためのもの)
グループ化だけを行い、参照を無効ににしたい場合、
( )を(?: )にすればよいらしい。
■ [基礎][正規表現]括弧に対する特殊変数$1、$2、$3について
( )を使うと、マッチング変数$`、$&、$'が無効になるかわりに、特殊変数$1、$2、、、が使えるようになる。
( )を入れ子にした場合は、外側から$1、$2、$3となる。
※ 一般的な正規表現だと\1とか\2を使うところが、perlでは$1(ダラー)とかになるので注意!
・サンプルコード
~$ cat hoge.pl #!/usr/local/bin/perl $string = "Japan Tokyo Tiyoda"; if ($string =~ /((\w+) (\w+) (\w+))/) { print '$1は、', $1, "です", "\n"; print '$2は、', $2, "です", "\n"; print '$3は、', $3, "です", "\n"; print '$4は、', $4, "です", "\n"; }
・実行結果
~$ ./hoge.pl $1は、Japan Tokyo Tiyodaです $2は、Japanです $3は、Tokyoです $4は、Tiyodaです