Perl has a rich set of built-in functions requiring no import. Special variables like $_, @_, $/, and %ENV make Perl scripts highly concise.
| Name | Signature | Description |
|---|---|---|
| print LIST | Print to stdout (no newline) | |
| say | say LIST | Print with newline (use feature 'say') |
| printf | printf FORMAT, LIST | Formatted print |
| sprintf | sprintf FORMAT, LIST → $str | Format to string |
| chomp | chomp $str / @arr | Remove trailing newline in-place |
| chop | chop $str → $char | Remove and return last character |
| length | length $str → $n | String length in characters |
| substr | substr $str, OFFSET, LEN → $sub | Extract/replace substring |
| index | index $str, $sub → $pos | Position of first occurrence |
| rindex | rindex $str, $sub → $pos | Position of last occurrence |
| uc / lc | uc $str / lc $str → $str | Uppercase / lowercase |
| ucfirst/lcfirst | ucfirst $str → $str | Capitalize / lowercase first char |
| reverse | reverse @arr / $str → list/str | Reverse list or string |
| split | split /PATTERN/, $str → @arr | Split string by regex or string |
| join | join $sep, @arr → $str | Join array elements with separator |
| push/pop | push @arr, LIST / pop @arr | Append / remove from end |
| shift/unshift | shift @arr / unshift @arr, LIST | Remove/add at beginning |
| grep | grep { COND } @arr → @filtered | Filter array elements |
| map | map { EXPR } @arr → @transformed | Transform array elements |
| sort | sort { $a <=> $b } @arr → @sorted | Sort array |
| scalar | scalar @arr → $count | Array length in scalar context |
| defined | defined $var → bool | True if variable is defined |
| exists | exists $hash{key} → bool | True if hash key exists |
| delete | delete $hash{key} → $val | Remove hash element |
| keys | keys %hash → @keys | Return list of hash keys |
| values | values %hash → @vals | Return list of hash values |
| each | each %hash → ($key, $val) | Iterate hash key-value pairs |
| die | die $msg | Throw exception (sets $@) |
| warn | warn $msg | Print warning to stderr |
| eval | eval { BLOCK } → $result | Catch exceptions (check $@) |
| my | my $var | Lexically scoped variable |
| local | local $var = VALUE | Temporary local value |
| our | our $var | Package global variable |
| use strict | use strict | Enable strict variable declarations |
| use warnings | use warnings | Enable runtime warnings |
| use feature | use feature 'say', 'state' | Enable modern Perl features |
| $_ | special variable | Default variable for many functions |
| @_ | special variable | Subroutine arguments |
| $! | special variable | System error message (errno) |
| %ENV | special variable | Environment variables hash |
| @ARGV | special variable | Command-line arguments |
| $0 | special variable | Script name |
Run with perl script.pl.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my $name = "MyWebUniversity";
my @langs = qw(Perl Python Ruby Rust Go Java R);
my %scores = (Alice => 95, Bob => 87, Carol => 92, Dave => 78);
say "Hello from $name!";
say "Languages: " . join(", ", @langs);
say "Total: " . scalar(@langs) . " languages";
# grep, map, sort
my @long = grep { length($_) > 3 } @langs;
my @upper = map { uc($_) } @long;
my @sorted = sort @upper;
say "Long uppercased: @sorted";
# Hash iteration sorted by value descending
say "=== Scores ===";
for my $n (sort { $scores{$b} <=> $scores{$a} } keys %scores) {
printf " %-10s %d\n", $n, $scores{$n};
}
# Subroutine
sub factorial {
my ($n) = @_;
return 1 if $n <= 1;
return $n * factorial($n - 1);
}
say "10! = " . factorial(10);
# String operations
my $text = " Hello, Perl World! ";
$text =~ s/^\s+|\s+$//g;
say "Trimmed : '$text'";
say "Upper : " . uc($text);
(my $modified = $text) =~ s/Perl/Modern Perl/;
say "Modified: $modified";
# perl core_demo.pl