What are special literals in Perl 5.3.4?

Special Literals in Perl are literals that represent different information about the source code. Following are the special literals in Perl:

Special Literal Description
__LINE__ Contains the current line number.
__FILE__ Contains the name of the file.
__END__ Is used to denote the end of the code.
__DATA__ Is used to represent a special file-handle.
__PACKAGE__ Contains the name of the current package. By default, the value is main.

Code

The following code presents an example that uses Special Literals:

print "Name of package: ", __PACKAGE__, "\n";
print "Name of file: ", __FILE__, "\n";
print "Line number: ", __LINE__, "\n";

Explanation

In the example above, three special literals are used. The first literal __PACKAGE__ displays main since we didn’t specify the package’s name. Therefore the default name main is used. The second literal __FILE__ outputs the file’s name main.perl. The last literal, __LINE__ displays the line number on which this literal is used. In this case, it is 3. Therefore it is output on the console.

Free Resources