Hidden features of Perl?

What are some really useful but esoteric language features in Perl that you've actually been able to employ to do useful work?

Guidelines:

  • Try to limit answers to the Perl core and not CPAN
  • Please give an example and a short description

Hidden Features also found in other languages' Hidden Features:

(These are all from Corion's answer)

  • C
    • Duff's Device
    • Portability and Standardness
  • C#
    • Quotes for whitespace delimited lists and strings
    • Aliasable namespaces
  • Java
    • Static Initalizers
  • JavaScript
    • Functions are First Class citizens
    • Block scope and closure
    • Calling methods and accessors indirectly through a variable
  • Ruby
    • Defining methods through code
  • PHP
    • Pervasive online documentation
    • Magic methods
    • Symbolic references
  • Python
    • One line value swapping
    • Ability to replace even core functions with your own functionality

Other Hidden Features:

Operators:

  • The bool quasi-operator
  • The flip-flop operator
    • Also used for list construction
  • The ++ and unary - operators work on strings
  • The repetition operator
  • The spaceship operator
  • The || operator (and // operator) to select from a set of choices
  • The diamond operator
  • Special cases of the m// operator
  • The tilde-tilde "operator"

Quoting constructs:

  • The qw operator
  • Letters can be used as quote delimiters in q{}-like constructs
  • Quoting mechanisms

Syntax and Names:

  • There can be a space after a sigil
  • You can give subs numeric names with symbolic references
  • Legal trailing commas
  • Grouped Integer Literals
  • hash slices
  • Populating keys of a hash from an array

Modules, Pragmas, and command-line options:

  • use strict and use warnings
  • Taint checking
  • Esoteric use of -n and -p
  • CPAN
  • overload::constant
  • IO::Handle module
  • Safe compartments
  • Attributes

Variables:

  • Autovivification
  • The $[ variable
  • tie
  • Dynamic Scoping
  • Variable swapping with a single statement

Loops and flow control:

  • Magic goto
  • for on a single variable
  • continue clause
  • Desperation mode

Regular expressions:

  • The \G anchor
  • (?{}) and '(??{})` in regexes

Other features:

  • The debugger
  • Special code blocks such as BEGIN, CHECK, and END
  • The DATA block
  • New Block Operations
  • Source Filters
  • Signal Hooks
  • map (twice)
  • Wrapping built-in functions
  • The eof function
  • The dbmopen function
  • Turning warnings into errors

Other tricks, and meta-answers:

  • cat files, decompressing gzips if needed
  • Perl Tips

See Also:

  • Hidden features of C
  • Hidden features of C#
  • Hidden features of C++
  • Hidden features of Java
  • Hidden features of JavaScript
  • Hidden features of Ruby
  • Hidden features of PHP
  • Hidden features of Python
  • Hidden features of Clojure

The flip-flop operator is useful for skipping the first iteration when looping through the records (usually lines) returned by a file handle, without using a flag variable:

while(<$fh>)
{
  next if 1..1; # skip first record
  ...
}

Run perldoc perlop and search for "flip-flop" for more information and examples.


There are many non-obvious features in Perl.

For example, did you know that there can be a space after a sigil?

 $ perl -wle 'my $x = 3; print $ x'
 3

Or that you can give subs numeric names if you use symbolic references?

$ perl -lwe '*4 = sub { print "yes" }; 4->()' 
yes

There's also the "bool" quasi operator, that return 1 for true expressions and the empty string for false:

$ perl -wle 'print !!4'
1
$ perl -wle 'print !!"0 but true"'
1
$ perl -wle 'print !!0'
(empty line)

Other interesting stuff: with use overload you can overload string literals and numbers (and for example make them BigInts or whatever).

Many of these things are actually documented somewhere, or follow logically from the documented features, but nonetheless some are not very well known.

Update: Another nice one. Below the q{...} quoting constructs were mentioned, but did you know that you can use letters as delimiters?

$ perl -Mstrict  -wle 'print q bJet another perl hacker.b'
Jet another perl hacker.

Likewise you can write regular expressions:

m xabcx
# same as m/abc/

Add support for compressed files via magic ARGV:

s{ 
    ^            # make sure to get whole filename
    ( 
      [^'] +     # at least one non-quote
      \.         # extension dot
      (?:        # now either suffix
          gz
        | Z 
       )
    )
    \z           # through the end
}{gzcat '$1' |}xs for @ARGV;

(quotes around $_ necessary to handle filenames with shell metacharacters in)

Now the <> feature will decompress any @ARGV files that end with ".gz" or ".Z":

while (<>) {
    print;
}