perl [オプション] プログラムファイル 引数...
レポート記述言語 perl(Practical Extraction and Report Language) は元々awkのように、テキストファイルを処理するために生まれたインタープリタ言語である。 しかし、今では数々の機能が追加されて、C言語でできることは(OpenGLなどを除いて)ほとんどできるほどの多機能な言語となっている。 文法は sed, awk, C言語をミックスして、+αしたような独特のものになっている。
perlの変数はいつでも
$(変数名)
の形で表される。
変数には変数宣言は不用。
型の区別もない。
配列の全体は
@(配列名)
の形で表される。
配列のようその個数は
$#(配列名)
で参照できる。
連想配列の全体は %配列名 の形で表される。
;# convert a ppm file to a ppm file <- コメント
if($#ARGV != 1){
die STDERR "Usage : jperl ",$0," ppmfile ppmfile\n";
}
open(inpic,"$ARGV[0]");
open(outpic,">$ARGV[1]");
select(outpic); <- 以降の出力にはoutpicを使用
;# read and write ppm header
$magic = <inpic> <- inpicから一行読み込んで変数$magicに代入
print $magic;
$res = <inpic> <- inpicから一行読み込んで変数$resに代入
print $res;
$col = <inpic> <- inpicから一行読み込んで変数$colに代入
print $col;
while(read(inpic,$rgb,3)){
($red,$green,$blue) = unpack('CCC',$rgb);
$red /= 2;
$green /= 2;
$blue /= 2;
$rgb = pack('CCC',$red,$green,$blue);
print $rgb;
}
実行する時は、上のプログラムを harf.pl として、以下のように実行する。
# perl harf.pl in.ppm out.ppm