SYNTAX
関数(function)
sub func
{
print( $_[0] );
print( $_[1] );
print( $_[2] );
my ( $a, $b ) = @_;
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 ++;
}
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;
コマンド置換
バッククォート内の文字列をシェルで実行して結果を返す。
shell のコマンド置換と同じ
my $s=`date`;
print($s);
POINT
shell コマンドの機能を利用できる。
my @a=`find -type f`;
print(@a);