Perl(1)

http://www.cpan.org/
http://kobesearch.cpan.org/

  • Support

http://www.pm.org
http://www.perlmonks.org

  • 1章

 @lines = `perldoc -u -f atan2`;  実行できず。チェック要。
 foreach (@lines){
  s/\w<([^>]+)>/\U$1/g;
  print;
 }
練習問題1-6へ

 整数リテラル 61_298_040 下線_をはさんで読みやすく出来る
 8進数 0377 :0をつける。
 シングルquote文字列: 'Don\'t' \'を使う。
 ダブルquote文字列: 逆スラッシュエスケープ まとめること。
  \" \\ とかで " 、\ を表現できる。
 文字列演算子
  連結(.演算子)    "hello" . "world"
  文字列繰り返し演算子  "fred" x 3 #fredfredfred
               5 x 4 # 5 * 4 でない
 組み込みの警告メッセージ
 %> perl -w my_program
 #!/usr/bin/perl -w
 レキシカル警告 バージョン5.6 -wよりもきめ細かな設定ができる
 2項代入演算子
  $fred +=5;
  $barney *= 3;
  $str .= " ";
 printによる出力(2.5.4)
  print "The answer is ", 6 * 7,".\n";
  コンマを区切って、printに複数の値を渡すことが出来る。(リストと呼ばれる)
 ダブルquote展開
  $what = "steak";
  print "fred ate ${what}s.\n";
 演算子の優先順位と結合(2.5.6)
 比較演算子
  数値演算子  == != < > <= >=
  文字列演算子 eq ne lt gt le ge
 if制御構造
  if(...) {
  }else{
  }
 ブール値
  undef :偽
  数値0 :偽 それ以外真
  空文字列('') 偽 それ以外真
  但し、'0' ゼロの文字列は、偽。数値と同じ。
 標準入力
   キーボードから値を入力(行入力演算子、改行文字まで)
 chomp演算子
  chomp($text = ); #テキストを読み込む。末尾の改行は削除
 while制御構造
  while ($count < 10) {
   ...
  }
 defined関数
  undefであって、空文字ではないことを確かめるために使う。引数がundefなら偽を返す。

  • 3章 リストと配列

  配列の最後の要素 rocksという配列の最後の要素のindexは、$#rocks
   (1,2,3)
   (1..100)# 100個の整数のリスト 範囲演算子 ..
   ($a..$b) #変数で定義するのもあり。
 qwショートカット
  qw/ fred barney betty wilma dino /
  シングルquoteで囲まれた文字列と見なすため、\n $fredは使えない。
  任意のデリミタ
   qw! fred barney betty !
   qw( ) qw{ } 
  qw! yahoo\! google excite !
  qw{
    /usr/dict/words
    /home/rootbeer/ .ispell_english
   }
  @stuff = (@giant,undef,@giant);  明示的にundefを挿入
 pop演算子とpush演算子
  @array = 5..9;
  $fred = pop(@array);
  $barney = pop @array;
  push(@array, 0);
  push @array,1..10;
  後ろの要素に作用する。
 shift演算子とunshift演算子
  @array = qw# dino fred barney #;
  $a = shift(@array); # fred barney
  unshift(@array,5)
  前の要素に作用する
 配列を文字列の中に展開する
  @rocks =qw{ flintstone state rubble };
  print "quarts @rocks limestone\n";
  自動的に間にスペースをいれて展開される。しかしダブルquoteしないとスペースが入らない。
  @varは、ダブルquoteで展開されるので展開されたくない場合は、\@とすること
  "this is ${fred}[3]\n"; # @fred でなく、$fredを表示させたい場合
  "this is $fred\[3]\n";
 foreach制御構造
  foreach $rock (qw/ bedrock slate lava /){
    print "One rock is $rock.\n";
  }
  foreach(1..10){
   print "I can count to $_!\n";
  }
   # $_が制御変数として使える。
 reverse演算子
  @fred = 6..10;
  @barney = reverse(@fred);
  @wilma = reverse 6..10;
 sort演算子
  @sorted = sort(@rocks);
 スカラーコンテキストとリストコンテキスト
  sort something # somethingはリストで無いと駄目
  リストを生成する式をスカラーコンテキストで使う p62
  スカラーを生成する式をリストコンテキストで使う p63
   @wilma = undef; # undef の要素が得られた
   @betty = (); #配列を空にする正しい方法
  スカラーコンテキストを強制
    print "I have ", scalar @rocks, "rocks!\n"; #数を表示する
 リストコンテキストでを使う
   chomp(@lines = ); #行をすべて読み込む、但し改行文字は除く
 print @val; をチェックすること!

  • 4章

 sub sum_of_fred_and_barney{
  print "Hey, you called the sum_of_fred_and_barney subroutine!\n";
  $fred + $ barney; # これが戻り値になる
 }
 サブルーチンを起動する
  &marine;
  $c = &sum_of_fred_and_barney;
 サブルーチンの引数は、自動的に @_に格納される。
 また、最初の引数は、$_[0], $_[1]など
 @_は、サブルーチン起動の度に新しいものが生成される。
 レキシカル変数: my
  my($a,$b) = @_;
 local演算子
  local($a,$b) = @_;
 my と localの相違(p76)
 可変長のパラメータリスト
  sub max{
   if (@_ !=2) {
      print "Warning...
}
 use strict;
 return演算子