3.2.123 require
require
This function asserts a dependency of some kind on its argument.
(If If the argument is a string, this function includes and executes the Perl code found in the separate file whose name is given by the string. This is similar to performing an eval on the contents of the file, except that require checks to see that the library file has not been included already. (It can thus be used to express file dependencies without worrying about duplicate compilation.) The function also knows how to search the include path stored in the @INC array (see the section "Special Variables" in Chapter 2 ). This form of the require function behaves much like this subroutine: sub require { my($filename) = @_; return 1 if $INC{$filename}; my($realfilename, $result); ITER: { foreach $prefix (@INC) { $realfilename = "$prefix/$filename"; if (-f $realfilename) { $result = eval `cat $realfilename`; last ITER; } } die "Can't find $filename in \@INC"; } die $@ if $@; die "$filename did not return true value" unless $result; $INC{$filename} = $realfilename; return $result; }
Note that the file must return true as the last value to indicate
successful execution of any initialization code, so it's customary to
end such a file with
This operator differs from the now somewhat obsolete
If
require
's argument is a number, the
version number of the currently executing Perl binary (as known by
$]
) is compared to require 5.003; and earlier versions of Perl will abort.
If
require
's argument is a package name (see
package
),
require
assumes an automatic require Socket; # instead of "use Socket;" However, one can get the same effect with the following, which has the advantage of giving a compile-time warning if Socket.pm can't be located: use Socket (); |
|