Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

import

perl

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.

like image 628
bogdan Avatar asked Oct 03 '10 15:10

bogdan


2 Answers

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?

like image 162
rafl Avatar answered Sep 23 '22 05:09

rafl


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.

like image 33
user465230 Avatar answered Sep 19 '22 05:09

user465230