Input and Output Basics
Default variable (topic) Current input line number Input record separator (default "\n"; undef = slurp mode)
Output record separator (appended by print) Output field separator for print
Separator when interpolating arrays (default " ")
use v5.36;
print "Enter your name: ";
my $name = <STDIN>; # reads one line, including the trailing newline
chomp $name; # remove the newline
chomp(my $age = <STDIN>); # common idiom
say "Hello, $name ($age)";
print STDOUT "Normal output\n";
print STDERR "Error output\n"; # or: warn "message\n";
printf "%-10s|%5d|%8.2f\n", 'item', 42, 3.14159;
my $formatted = sprintf "%03d", 7; # "007"
# Read all lines from files named on the command line (or STDIN): the diamond operator
while (my $line = <>) {
chomp $line;
say "$.: $line"; # $. = current line number
}
# Safer double-diamond (5.22+): doesn't treat filenames specially (e.g., "| cmd")
while (<<>>) { print }
$| = 1; # autoflush STDOUT (useful for progress output)
STDOUT->autoflush(1); # the same, OO style