Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

require

Loads and executes a Perl file or module, caching it in %INC so that subsequent calls for the same module are no-ops.

Behaviour depends on the argument type:

  1. Version number (numeric or v-string): asserts that the Perl version is at least that value. pperl claims 5.42 compatibility, so any 5.x requirement succeeds; 6+ dies.

  2. Bareword / string: converts Foo::Bar to Foo/Bar.pm, then searches @INC for the file. If already present in %INC, the call is a no-op. Otherwise the file is compiled and executed in a sub-interpreter, subs are registered, and %INC is updated.

Dies with "Can't locate ..." if the file is not found. Returns 1 on success.

Synopsis

require 5.036;              # version check
require Carp;               # load Carp.pm via @INC
require "config.pl";        # load a file by path

Implementation Notes

perl5 (pp_ctl.c:4059):

  1. Get filename from stack
  2. Check %INC — skip if already loaded
  3. Search @INC for the file
  4. Compile and execute the file
  5. Record in %INC, push 1 on success

pperl p5: compile file via builder bridge, merge subs, execute.

See Also

PP runtime: require | use, do, eval, @INC, %INC