Perl tokens you should know

Perl has many global variables, a few of which are stored in a special literal format as: __NAME__. It’s good to be aware of these special literals, (aka tokens) as they appear frequently in Perl code and provide useful functionality.


This token contains the name of the package which is declared at the top of any Perl module e.g:

package Perltricks::Example;
use strict;
use warnings;

sub print_package_name {
    print __PACKAGE__ . "\n";

In this example, the subroutine ‘print_package_name’ would print ‘Perltricks::Example’. __PACKAGE__ is one of the most useful (and frequently used) tokens, as it has applications in code generation and class inheritance, where the programmer does not know the name of the package ahead of time. In a Perl program (.pl file) __PACKAGE__ returns ‘main’.


The __LINE__ token returns the value of the current line number. __FILE__ provides the filename. Similar to __PACKAGE__ these tokens can be used with string and numeric functions as appropriate (such as print).


__END__ defines the end of the Perl code in the file. Any text that appears after __END__ is ignored by the Perl compiler. Perl programmers often put module documentation after an __END__ token. Even though POD markup language is ignored by the Perl compiler, using __END__ provides the guarantee that even if the POD markup contains a syntax error, the Perl compiler will not scan that part of the file. A clear example of that can be seen in the LWP source code.

__DATA__ is similar to __END__ in that it defines the end of the Perl code in any file. However, any text that appears on the line immediately after __DATA__ until the end of the file, is read into the filehandle PACKAGENAME::DATA, (where package name is the name of the package that __DATA__ appeared in). The documentation for SelfLoader discusses __DATA__ and __END__ in more detail.


__SUB__ returns a reference to the current subroutine. It’s available in Perl 5.16 and higher via the ‘use feature’ pragma.

*This article was updated on 15th May 2013 including corrections to information relating to __SUB__. Thanks to Jochen Hayek for the correction.*

This article was originally posted on


David Farrell

David is a professional programmer who regularly tweets and blogs about code and the art of programming.

Browse their articles


Something wrong with this article? Help us out by opening an issue or pull request on GitHub