How do I use Perl's import, use, require and do?
Asked Answered
D

4

7

Can someone explain exactly the usage recomandations regarding the 4 perl imports: do, import, use and require?

I'm looking for practical recommendations and keeping in mind possible issues that might arise in the context of mod_perl or something similar.

We all love simple examples, good ones!

So far the best resource I found was http://soniahamilton.wordpress.com/2009/05/09/perl-use-require-import-and-do/ , but this missed to consider the implications of mod_perl.

Diazomethane answered 3/10, 2010 at 15:35 Comment(3)
How is Quick notes for me; not meant to be authoritative "the best resource" you found?Cuprous
We have an entire chapter on this in Intermediate Perl. :)Dippy
#2181054 This is a better post Please refer it.Fritzie
C
14

You should first read perldoc -f use and perldoc -f require.

They are excellent resources and explain how use works, how it invokes import and then require, and how you could theoretically implement require in terms of do.

If you have already read them, do you still have any specific open questions that the standard documentation doesn't cover well enough and you would like to have answered in more detail?

Cleisthenes answered 3/10, 2010 at 15:43 Comment(0)
S
14

do will call the code, no ifs, ands, or buts, at runtime. This is usually a bad idea, because if that's happening, you should really probably be putting it into a subroutine.

require will call exactly once and then no more, at runtime. it can do it for a package, too, in which case it will actually go find that package for you.

use does everything require does in the package case, then calls import in that package.

import is a function defined in a package. it gets called by use, but it's not otherwise special.

Scopoline answered 3/10, 2010 at 15:43 Comment(1)
You forgot to mention that use happens at compile time. use Foo; is equivalent to BEGIN { require Foo; Foo->import(); }Photographic
C
14

You should first read perldoc -f use and perldoc -f require.

They are excellent resources and explain how use works, how it invokes import and then require, and how you could theoretically implement require in terms of do.

If you have already read them, do you still have any specific open questions that the standard documentation doesn't cover well enough and you would like to have answered in more detail?

Cleisthenes answered 3/10, 2010 at 15:43 Comment(0)
P
2

You can look at the mod_perl documentation for use(), require(), do()

Potted answered 3/10, 2010 at 16:47 Comment(0)
D
-2

Since this question didn't gather more than RTFM as an 'answer':

Create a file called Example.pm in the same directory (or relative, like ./lib) and add these lines

package Example;
use Exporter qw(import);
our @EXPORT_OK = qw(subroutine1 subroutine2 etc)
1;

In your main script add these lines

use lib '.';
use Example qw(subroutine1 subroutine2 etc);

Subroutines named in both export_ok and use qw lists are available in the main script.

Bart...

Dolor answered 11/2, 2023 at 17:42 Comment(1)
This doesn't answer the question. Where's the part about do? Or require? or import?Viscometer

© 2022 - 2024 — McMap. All rights reserved.