← Back to Perl Course | Chapter 8: Modules | Lesson 1 of 7

use और require

use compile time पर एक module load करता है, और require इसे program चलते वक्त load करता है।

In this page:

  1. use और require

use और require

use Module LIST एक require के बाद module के import method की एक call के बराबर है, और यह एक implicit BEGIN block के अंदर compile time पर होता है। require Module इसे run time पर load करता है और कुछ import नहीं करता, इसलिए आप functions को इनके full names से call करते हैं। क्योंकि require fail हो सकता है, यह अक्सर एक optional module को सिर्फ तब load करने के लिए एक eval में रखा जाता है जब यह exist करे।

Note: use Module () बिना import call किए module load करता है, जबकि use Module qw(name) सिर्फ named functions import करता है।

उदाहरण: use and require

perl
use strict;
use warnings;
use List::Util qw(sum);

print "use imported sum: ", sum(1, 2, 3), "\n";

require File::Basename;
print "basename: ", File::Basename::basename("/tmp/report.txt"), "\n";

my $has_missing = eval { require Some::Missing::Module; 1 } ? "yes" : "no";
print "Some::Missing::Module available: $has_missing\n";
my $has_posix = eval { require POSIX; 1 } ? "yes" : "no";
print "POSIX available: $has_posix\n";
BEGIN { print "this line runs first, at compile time\n"; }
print "then the normal code runs\n";

# Output:
# this line runs first, at compile time
# use imported sum: 6
# basename: report.txt
# Some::Missing::Module available: no
# POSIX available: yes
# then the normal code runs
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि use compile time की बजाय उस जगह चलेगा जहाँ यह लिखा है
  2. बिना import किए require के बाद imported function names call करना
  3. जब आपको सिर्फ एक या दो functions चाहिए सब कुछ import करना
चैप्टर सारांश
  • use compile time पर चलता है और import करता है
  • require run time पर चलता है
  • eval { require Module } एक module test करता है
  • सिर्फ ज़रूरी names import करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.