if/unless/while/until/foreach等は、「修飾子」にできる:
#!/usr/bin/env perl use v5.12; use warnings; my @array = (1, 2, 3); say foreach (@array);
ブロックスコープ:
#!/usr/bin/env perl use v5.12; use warnings; { print "Please enter a number: "; chomp(my $n =); my $root = sqrt $n; # $root はこのブロック内でのみ有効な変数 say "The square root of $n is $root"; }
last/next/redo:
- for/foreach/while/until/裸のブロックから抜け出せる
- if/until/サブルーチンブロックからは抜け出せない
例:
#!/usr/bin/env perl use v5.12; use warnings; foreach (1..10) { print "Iteration number $_.nn"; print "Please choose: last, next, redo, or none of the above? "; chomp(my $choice =); print "n"; last if $choice =~ /last/i; next if $choice =~ /next/i; redo if $choice =~ /redo/i; print "That wasn't any of the choices... onward!nn"; } print "That's all, folks!n";
defined-or演算子(左辺が未定義なら右辺の値を返す):
#!/usr/bin/env perl use v5.12; use warnings; my %last_name; my $last_name = $last_name{'someone'} // '{No last name}'; say $last_name;