Social Icons

Pages

Friday, 26 September 2014

Difference Between use and require in perl

This question is asked in many technical interviews which required knowledge of perl.

Require:

  1. Usage: require EXPR
  2. if EXPR is a numeric value, then script demands that i requires specified version of perl.
  3. if EXPR is not numeric, then i assumes that EXPR is name of file to be included in script.
  4. The included file must return true as last statement.
  5. file extension should be specified.
  6. Can be used to load libraries as well as packages.
  7. file will be loaded and verified at run time.
  8. require creates namespace with name as EXPR and function and variables of package will be referred as EXPR::variable or EXPR::function
  9. package name should be specified with .pm extension

Use:

  1. Usage: use EXPR
  2. if EXPR is a numeric value, then script demands that i requires specified version of perl.
  3. if EXPR is not numeric, then i assumes that EXPR is name of file to be included in script.
  4. The included file must return true as last statement.
  5. file extension need not to be specified.
  6. Can be used to import packages only.
  7. package will be loaded and verified at compile time.
  8. No new namespace will be created. All functions and variables from package will be added to main namespace.
  9. use "Module" is equivalent to:

require "Module.pm"
Module->import()

No comments:

Post a Comment

Please Comment for Any question or suggestions