@INC और Modules ढूँढना
Perl आपके load किए हर module को ढूंढने के लिए directories की एक list, @INC, search करता है।
In this page:
@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
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
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- file का नाम package name से अलग रखना
- run time पर @INC बदलना और फिर use इस्तेमाल करना
- यह उम्मीद करना कि एक 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: