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

@INC और Modules ढूँढना

Perl आपके load किए हर module को ढूंढने के लिए directories की एक list, @INC, search करता है।

@INC और Modules ढूंढना

जब आप use Some::Module लिखते हैं, Perl नाम को Some/Module.pm में convert करता है और इसे @INC की हर directory में order से ढूंढता है। एक बार load होने पर, file %INC hash में record होती है ताकि यह दो बार load न हो। आप use lib dir से या require से पहले @INC बदलकर अपनी directory add कर सकते हैं, और PERL5LIB environment variable भी directories add करता है।

Note: use lib compile time पर चलता है, इसलिए यह एक use statement के साथ काम करता है, जबकि run time पर @INC बदलना सिर्फ बाद के एक require के लिए मदद करता है।

उदाहरण: @INC and finding modules

perl
use strict;
use warnings;

my $dir = "demo_lib";
mkdir $dir unless -d $dir;
open(my $fh, '>', "$dir/Hello.pm") or die "Cannot write module: $!";
print $fh "package Hello;\nsub greet { return 'Hello from a module file'; }\n1;\n";
close $fh;

print "Hello.pm loaded before? ", (exists $INC{'Hello.pm'} ? "yes" : "no"), "\n";
unshift @INC, $dir;
require Hello;
print Hello::greet(), "\n";
print "loaded from: $INC{'Hello.pm'}\n";
print "first \@INC entry: $INC[0]\n";
print "module path for Data::Dumper: ", join('/', split(/::/, 'Data::Dumper')) . ".pm", "\n";

unlink "$dir/Hello.pm";
rmdir $dir;

# Output:
# Hello.pm loaded before? no
# Hello from a module file
# loaded from: demo_lib/Hello.pm
# first @INC entry: demo_lib
# module path for Data::Dumper: Data/Dumper.pm
{# 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. file का नाम package name से अलग रखना
  2. run time पर @INC बदलना और फिर use इस्तेमाल करना
  3. यह उम्मीद करना कि एक module दूसरे require से reload होगा
चैप्टर सारांश
  • @INC search की गई directories list करता है
  • %INC loaded files record करता है
  • use lib एक directory add करता है
  • Module names file paths में map होते हैं
🔒

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.