Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Perl Index

🐪 Core Perl — Built-in functions, variables, and control structures

Perl has a rich set of built-in functions requiring no import. Special variables like $_, @_, $/, and %ENV make Perl scripts highly concise.

📋 Functions & Syntax

NameSignatureDescription
printprint LISTPrint to stdout (no newline)
saysay LISTPrint with newline (use feature 'say')
printfprintf FORMAT, LISTFormatted print
sprintfsprintf FORMAT, LIST → $strFormat to string
chompchomp $str / @arrRemove trailing newline in-place
chopchop $str → $charRemove and return last character
lengthlength $str → $nString length in characters
substrsubstr $str, OFFSET, LEN → $subExtract/replace substring
indexindex $str, $sub → $posPosition of first occurrence
rindexrindex $str, $sub → $posPosition of last occurrence
uc / lcuc $str / lc $str → $strUppercase / lowercase
ucfirst/lcfirstucfirst $str → $strCapitalize / lowercase first char
reversereverse @arr / $str → list/strReverse list or string
splitsplit /PATTERN/, $str → @arrSplit string by regex or string
joinjoin $sep, @arr → $strJoin array elements with separator
push/poppush @arr, LIST / pop @arrAppend / remove from end
shift/unshiftshift @arr / unshift @arr, LISTRemove/add at beginning
grepgrep { COND } @arr → @filteredFilter array elements
mapmap { EXPR } @arr → @transformedTransform array elements
sortsort { $a <=> $b } @arr → @sortedSort array
scalarscalar @arr → $countArray length in scalar context
defineddefined $var → boolTrue if variable is defined
existsexists $hash{key} → boolTrue if hash key exists
deletedelete $hash{key} → $valRemove hash element
keyskeys %hash → @keysReturn list of hash keys
valuesvalues %hash → @valsReturn list of hash values
eacheach %hash → ($key, $val)Iterate hash key-value pairs
diedie $msgThrow exception (sets $@)
warnwarn $msgPrint warning to stderr
evaleval { BLOCK } → $resultCatch exceptions (check $@)
mymy $varLexically scoped variable
locallocal $var = VALUETemporary local value
ourour $varPackage global variable
use strictuse strictEnable strict variable declarations
use warningsuse warningsEnable runtime warnings
use featureuse feature 'say', 'state'Enable modern Perl features
$_special variableDefault variable for many functions
@_special variableSubroutine arguments
$!special variableSystem error message (errno)
%ENVspecial variableEnvironment variables hash
@ARGVspecial variableCommand-line arguments
$0special variableScript name

💡 Example

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

🏠 Index  |  Regex →