Perl Programming/Keywords/require

Previous: rename Keywords Next: reset

The require keyword edit

The require keyword demands a Perl VERSION, some semantics specified by EXPRESSION or which is in $_, if EXPRESSION is missing. VERSION may be either a numeric argument like as 5.006 that will be compared to $], or a literal of the form v5.6.1 that will be compared to $^V (= $PERL_VERSION). The literal form should be avoided, as it leads to misleading error messages in early Perl versions. If VERSION is larger than the version of the Perl interpreter, an exception is raised.

Without these parameters, require demands a file to be included, if it has not already been included before. The file will not be included twice under the same specified name. The included file must return true at the end, so, it is custom to end such files with a 1.

If the EXPRESSION is a bareword, require assumes a .pm extension, replacing all :: with / in the filename so that standard modules load easily.

Before searching for a file with the .pm extension, require searches first for a file with .pmc extension, loading it instead of the .pm file, if found.

Syntax edit

  require VERSION
  require EXPRESSION
  require

Examples edit

require v5.14.2;    # runtime version check
require 5.14.2;     # ditto
require 5.014_002;  # preferred for backwards compatibility

require Foo::Bar;   # a splendid bareword
# Will look for the "Foo/Bar.pm" file in the directories specified in the @INC array.

$class = 'Foo::Bar';
require $class;     # $class is not a bareword
require "Foo::Bar"; # not a bareword because of the ""
# Will look for the "Foo::Bar" file in the @INC array and will complain not finding it.

See also edit

Previous: rename Keywords Next: reset