Built-in Functions for Lists and Strings
$_ aliasing: in map, grep, and for, $_ is an alias to the original element. Modifying $_ modifies the source (s/// inside map changes the original! Use s///r instead).
use v5.36;
my @nums = (5, 3, 8, 1, 9, 2);
my @doubled = map { $_ * 2 } @nums; # transform
my @evens = grep { $_ % 2 == 0 } @nums; # filter
my %squares = map { $_ => $_ ** 2 } 1..5; # build a hash
my @pairs = map { [$_, length $_] } qw(a bb ccc);
my $evens_count = grep { $_ % 2 == 0 } @nums; # scalar context: count
my @files = grep { -f } glob '*'; # file test on $_
my @reversed = reverse @nums;
my $joined = join ', ', @nums;
my @parts = split //, 'abc'; # characters
my @fields = split /\t/, $line, -1; # -1 keeps trailing empty fields
my ($k, $v) = split /=/, 'key=value', 2; # limit to 2 fields
my @w = split ' ', ' a b c '; # special case: splits on whitespace, ignores leading space
use List::Util qw(first sum sum0 max min maxstr minstr reduce shuffle uniq uniqnum any all none pairs pairmap head tail zip mesh);
my $first_big = first { $_ > 4 } @nums; # 5
my $total = sum0 @nums;
my $product = reduce { $a * $b } @nums;
my @uniq = uniq qw(a b a c);
say 'has big' if any { $_ > 8 } @nums;
say 'all positive' if all { $_ > 0 } @nums;
my @top3 = head 3, sort { $b <=> $a } @nums;
m
…