top

TOP  RSS  Login

Perl/制御文

Perl >

if...elsif..else

my $var = (undef,"",1)[rand(3)];

if (!defined($var)) {
  print "undefined";
} elsif ($var eq ""){
  print "empty"
} else {
  print "$var";
}

# 等価
print
  !defined($var) ? "undefined" :
  $var eq ""     ? "empty" :
                   $var;

while

my $i = 0;
while (++$i<10) {
  last if($i == 5);
} continue {
  print "$i next";
}

for

for (my $i=0; $i<10; $i++){
  print $i;
}

foreach

my @a = (0..10);
foreach my $i (@a){
  print $i;
}

# 等価
for my $i (@a){
  print $i;
}

# 等価
print join("", @a);

Last modified:2007/01/14 14:10:05

cD

0.389517