Examples
Perl examples
Explore practical text processing and scripting.
Perl Basics
Declare a scalar and print it.
Perl Subroutines
Define a sub that reads its arguments from @_.
Perl regular expressions and text processing
Capture named groups, collect all matches, transliterate characters, and compute replacements with /e.
Perl references and nested data structures
Build hashes of arrays, add to them, and loop over them with references.
Perl file handles on strings and formatted output
Read lines from a string as if from a file, validate each line, and write formatted output to a string.
Perl subroutines, context, and closures
Return different results in list and scalar context, build closures, and write a recursive anonymous function.
Perl packages, bless, and inheritance
Create objects as blessed hashes, override methods in a subclass, and call the parent version.
Perl Arrays
Store a list in an @array and loop over it.
Perl Hashes
Map keys to values and print them in a stable order.
Perl sort blocks and the Schwartzian transform
Sort numbers, hash keys by value, and records by computed keys without recomputing the keys.
Perl stacks, queues, and word frequencies
Use push, pop, shift, and unshift as stack and queue operations, and count items with a hash.
Perl SHA-256 and HMAC with Digest::SHA
Hash messages, authenticate them with a key, and compare tags without leaking timing.