#! /usr/bin/perl # step 1 # only print strings. print "Hello Perl!\n";
# step 2 # using scalar variable $string = "step2"; print $string, "\n";
# step 3 # array @array = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); print "\@array =", @array, "\n"; print "\$array[1] =", $array[1], "\n"; print "\$#array =", $#array, "\n"; # slice print "\@array[1 .. 5] =", @array[1 .. 5], "\n"; print "\@array[4..6, 1] =", @array[4..6, 1], "\n";
# step 4 # associative array %record = ('Key', 'yaguchi-mari', 'Name', 'Yaguchi Mari', 'Sex', 'Female'); print "Please input field-name (Key, Name, Sex): "; chop($field =); printf("%s is %s\n", $field, $record{$field});
if (条件) { ... }
for (初期値; 条件; 増分) { ... }
while (条件) { ... }
foreach 変数 (配列またはリスト) { ... }
sub sub-routine-name { ... }引数の渡し方
&hoge(1, 2); sub hoge { local ($arg1, $arg2) = @_; print "$arg1 $arg2\n"; }
$ARGV[0] = 第1引数(C言語と異なる点に注意!)コマンドラインから与えられたスクリプト自身名は、$0に格納される。