Perl 5 version documentation

require

  • require VERSION

  • require EXPR
  • require

    Demands a version of Perl specified by VERSION, or demands some semantics specified by EXPR or by $_ if EXPR is not supplied.

    VERSION may be either a literal such as v5.24.1, which will be compared to $^V (or $PERL_VERSION in English), or a numeric argument of the form 5.024001, which will be compared to $] . An exception is raised if VERSION is greater than the version of the current Perl interpreter. Compare with use, which can do a similar check at compile time.

    Specifying VERSION as a numeric argument of the form 5.024001 should generally be avoided as older less readable syntax compared to v5.24.1. Before perl 5.8.0 (released in 2002), the more verbose numeric form was the only supported syntax, which is why you might see it in older code.

    1. require v5.24.1; # run time version check
    2. require 5.24.1; # ditto
    3. require 5.024_001; # ditto; older syntax compatible
    4. with perl 5.6

    Otherwise, require demands that a library file be included if it hasn't already been included. The file is included via the do-FILE mechanism, which is essentially just a variety of eval with the caveat that lexical variables in the invoking script will be invisible to the included code. If it were implemented in pure Perl, it would have semantics similar to the following:

    1. use Carp 'croak';
    2. use version;
    3. sub require {
    4. my ($filename) = @_;
    5. if ( my $version = eval { version->parse($filename) } ) {
    6. if ( $version > $^V ) {
    7. my $vn = $version->normal;
    8. croak "Perl $vn required--this is only $^V, stopped";
    9. }
    10. return 1;
    11. }
    12. if (exists $INC{$filename}) {
    13. return 1 if $INC{$filename};
    14. croak "Compilation failed in require";
    15. }
    16. foreach $prefix (@INC) {
    17. if (ref($prefix)) {
    18. #... do other stuff - see text below ....
    19. }
    20. # (see text below about possible appending of .pmc
    21. # suffix to $filename)
    22. my $realfilename = "$prefix/$filename";
    23. next if ! -e $realfilename || -d _ || -b _;
    24. $INC{$filename} = $realfilename;
    25. my $result = do($realfilename);
    26. # but run in caller's namespace
    27. if (!defined $result) {
    28. $INC{$filename} = undef;
    29. croak $@ ? "$@Compilation failed in require"
    30. : "Can't locate $filename: $!\n";
    31. }
    32. if (!$result) {
    33. delete $INC{$filename};
    34. croak "$filename did not return true value";
    35. }
    36. $! = 0;
    37. return $result;
    38. }
    39. croak "Can't locate $filename in \@INC ...";
    40. }

    Note that the file will not be included twice under the same specified name.

    The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements.

    If EXPR is a bareword, require assumes a .pm extension and replaces :: with / in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace, however it will autovivify the stash for the required module.

    In other words, if you try this:

    1. require Foo::Bar; # a splendid bareword

    The require function will actually look for the Foo/Bar.pm file in the directories specified in the @INC array, and it will autovivify the Foo::Bar:: stash at compile time.

    But if you try this:

    1. my $class = 'Foo::Bar';
    2. require $class; # $class is not a bareword
    3. #or
    4. require "Foo::Bar"; # not a bareword because of the ""

    The require function will look for the Foo::Bar file in the @INC array and will complain about not finding Foo::Bar there. In this case you can do:

    1. eval "require $class";

    or you could do

    1. require "Foo/Bar.pm";

    Neither of these forms will autovivify any stashes at compile time and only have run time effects.

    Now that you understand how require looks for files with a bareword argument, there is a little extra functionality going on behind the scenes. Before require looks for a .pm extension, it will first look for a similar filename with a .pmc extension. If this file is found, it will be loaded in place of any file ending in a .pm extension. This applies to both the explicit require "Foo/Bar.pm"; form and the require Foo::Bar; form.

    You can also insert hooks into the import facility by putting Perl code directly into the @INC array. There are three forms of hooks: subroutine references, array references, and blessed objects.

    Subroutine references are the simplest case. When the inclusion system walks through @INC and encounters a subroutine, this subroutine gets called with two parameters, the first a reference to itself, and the second the name of the file to be included (e.g., Foo/Bar.pm). The subroutine should return either nothing or else a list of up to four values in the following order:

  • A reference to a scalar, containing any initial source code to prepend to the file or generator output.

  • A filehandle, from which the file will be read.

  • A reference to a subroutine. If there is no filehandle (previous item), then this subroutine is expected to generate one line of source code per call, writing the line into $_ and returning 1, then finally at end of file returning 0. If there is a filehandle, then the subroutine will be called to act as a simple source filter, with the line as read in $_ . Again, return 1 for each valid line, and 0 after all lines have been returned. For historical reasons the subroutine will receive a meaningless argument (in fact always the numeric value zero) as $_[0] .

  • Optional state for the subroutine. The state is passed in as $_[1] .

  • If an empty list, undef, or nothing that matches the first 3 values above is returned, then require looks at the remaining elements of @INC . Note that this filehandle must be a real filehandle (strictly a typeglob or reference to a typeglob, whether blessed or unblessed); tied filehandles will be ignored and processing will stop there.

    If the hook is an array reference, its first element must be a subroutine reference. This subroutine is called as above, but the first parameter is the array reference. This lets you indirectly pass arguments to the subroutine.

    In other words, you can write:

    1. push @INC, \&my_sub;
    2. sub my_sub {
    3. my ($coderef, $filename) = @_; # $coderef is \&my_sub
    4. ...
    5. }

    or:

    1. push @INC, [ \&my_sub, $x, $y, ... ];
    2. sub my_sub {
    3. my ($arrayref, $filename) = @_;
    4. # Retrieve $x, $y, ...
    5. my (undef, @parameters) = @$arrayref;
    6. ...
    7. }

    If the hook is an object, it must provide an INC method that will be called as above, the first parameter being the object itself. (Note that you must fully qualify the sub's name, as unqualified INC is always forced into package main .) Here is a typical code layout:

    1. # In Foo.pm
    2. package Foo;
    3. sub new { ... }
    4. sub Foo::INC {
    5. my ($self, $filename) = @_;
    6. ...
    7. }
    8. # In the main program
    9. push @INC, Foo->new(...);

    These hooks are also permitted to set the %INC entry corresponding to the files they have loaded. See %INC in perlvar.

    For a yet-more-powerful import facility, see use and perlmod.