sonia hamilton – linux, brazilian jiu jitsu

9 May 2009

Perl – use, require, import, and do

Filed under: Uncategorized — Sonia Hamilton @ 21:43
Tags:

Some notes on Perl’s use, require, import and do. Quick notes for me; not meant to be authoritative…

  • use is done at ‘compile-time’ and require is done at ‘run-time’ (ie can conditionally load modules)
  • require is the older method, but use uses require to do it’s work:

use Foo;   # equivalent to:
require Foo; Foo->import();
.
use Foo qw (foo bar);   # equivalent to:
require Foo; Foo->import(qw(foo bar));
.
use Foo();   # equivalent to:
require Foo;   # ie don't import anything, not even the default things

  • a Library is a just a file of code; a Module has package Foo in it (and usually other stuff)
  • use only works with modules, whereas require works with both modules and libraries. Corollary: use only works with module names (Foo), whereas require also works with paths (eg ~/lib/foo.pm)
  • require Foo will check if Foo has already been loaded, whereas do Foo will unconditionally reload Foo
  • better practice is to write modules rather than librarys, to prevent namespace pollution. A simple module:

package Foo;   # minimal. Usually add things like:
use base qw (Exporter);
our @EXPORT = qw(qux slarken);   # keep this list small - namespace pollution

  • to use this module:

use lib '~/lib'; # add this to %INC
use Foo;   # loads module, imports symbols in @EXPORT
Foo->bar();   # correct
Foo::bar();   # works, but not for inherited methods
qux();   # works, due to export
bar();   # carks

Some links:

6 Comments »

  1. another side point is that in a cached environment (like mod_perl or a daemon), trying to ‘use’ a module or file over and over again wont achieve anything, as perl sees its already loaded and moves on. where as ‘require’ will load the file or module regardless.

    Foo::bar() directly references the bar sub inside Foo’s name space.

    so…

    package Foo;
    sub bar { return 1; }

    Foo->bar() is actually an OO call, and perl automatically passes the sub the persistent object as the first argument. Its fine to use it this way, but you have to remember to shift off the first argument. Its best to just make the module oo or not. And if not just import the functions you want in to your name space.

    ie

    use Foo qw(function1 function2);

    then function1() and function2() are imported.

    OO is a nice bag of beans. For a comprehensive discussion, check out

    http://perldoc.perl.org/perltoot.html

    Comment by Dean Hamstead — 12 May 2009 @ 08:35 | Reply

  2. Thank you for this simple, simple example. I needed to change a few existing files and don’t know perl. This is exactly what I needed.

    Comment by Matt — 16 October 2009 @ 06:57 | Reply

  3. good post 🙂
    i like it

    Comment by chhikv — 28 October 2009 @ 01:28 | Reply

  4. […] do executes the perl script as many times as you call it. In my case, I needed do.  Thanks to https://soniahamilton.wordpress.com/2009/05/09/perl-use-require-import-and-do/ Bookmark Posted by Paul Kiddie on Monday, January 18, 2010, at 9:51 am. Filed under perl. Tagged […]

    Pingback by Paul Kiddie’s Blog : Redirecting STDOUT to variable in perl and running child scripts — 18 January 2010 @ 19:51 | Reply

  5. nice bag of beans do executes the perl script as many times as you call it. In my case, I needed do. Thanks

    Comment by IN.MG Domain — 10 January 2011 @ 03:51 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: