Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change


by gods
on Aug 24, 1999 at 22:42 UTC ( #274=perlfunc: print w/replies, xml ) Need Help??


See the current Perl documentation for require.

Here is our local, out-dated (pre-5.6) version:

require - load in external functions from a library at runtime

require EXPR


Demands some semantics specified by EXPR, or by $_ if EXPR is not supplied. If EXPR is numeric, demands that the current version of Perl ( $] or $PERL_VERSION) be equal or greater than EXPR.

Otherwise, 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(). Has semantics similar to the following subroutine:

    sub require {
        my($filename) = @_;
        return 1 if $INC{$filename};
        ITER: {
            foreach $prefix (@INC) {
                $realfilename = "$prefix/$filename";
                if (-f $realfilename) {
                    $result = do $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 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, the 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.

In other words, if you try this:

        require Foo::Bar;    # a splendid bareword 

The require function will actually look for the ``Foo/'' file in the directories specified in the @INC array.

But if you try this:

        $class = 'Foo::Bar';
        require $class;      # $class is not a bareword
        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:

        eval "require $class";

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

Log In?

What's my password?
Create A New User
[Corion]: ambrus: Yes, ideally you would have the ease of overhead projection transparencies and pens drawing on them, combined with the computer generated slide text...
[Corion]: Maybe the solution would be a tablet (with pens), like the Wacom tablets, but you still need good software and need to know how to operate it well in an interactive setting ;)
[ambrus]: Corion: the solution depends on who for. Some non-math presentations need to show lots of graphics or photos, with fine details and color. For those, blackboard or overhead transparencies aren't so good (yes, you can print on transparencies, but there's s
[ambrus]: ome quality limits), but film projection or computer projector is fine.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2017-09-26 10:32 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.