SYNTAX





関数(function)


sub func { # Argument は $_ という配列にはいっている print( $_[0] ); print( $_[1] ); print( $_[2] ); # 変数をまとめてうけとる my ( $a, $b ) = @_; # Function 内で shift を使って 順に引数をとる my $a = shift; my $b = shift; # 返り値 my $ret = "aaa"; return $ret; } { # 呼び出し func("aaa", "bbb", "ccc"); }



制御構文





while


SYNTAX while cond { exp ... }
while ( $i < 10 ) { print "cnt $i \n"; $i ++; } # <FILEHANDLE> とすると 行がなくなるまで 1 行ずつ処理をする open ( FP, $ARGV[0] ) while ( <FP> ) { print "$_\n"; } close( FP );
WARNING loop を継続するには continue ではなく next を利用する。
if ( "aaa" == "aaa" ) { next; }



if


SYNTAX if ( EXPRESSION ) if ( EXPRESSION ) BLOCK else BLOCK if ( EXPRESSION ) BLOCK elsif BLOCK else BLOCK


foreach


SYNTAX foreach 制御変数 ( リスト値 ) { 実行 } foreach scalar ( array ) { statement }
foreach $i (3, 2, 1) { print "count $i \n"; }



require


SYNTAX require FILE DESC perl code を 読み込み評価する( 2 重に load されない ) include したいときに使う
sub func() { print "call func\n"; } # 最後に 1 を返すようにすること 1;



コマンド置換


バッククォート内の文字列をシェルで実行して結果を返す。 shell のコマンド置換と同じ
my $s=`date`; print($s);
POINT shell コマンドの機能を利用できる。
# ディレクトリのエントリをリストする。 my @a=`find -type f`; print(@a);