Perl 之 use(), require(), do(), %INC and @INC
来源:
http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html
转自:http://perl.apache.org/docs/general/perl_reference/perl_reference.html
use(), require(), do(), %INC and @INC Explained
The %INC hash
The following examples will make it easier to understand the logic.
First, let's see what are the contents of @INC
on my system:
% perl -e 'print join "\n", @INC'/usr/lib/perl5/5.00503/i386-linux/usr/lib/perl5/5.00503/usr/lib/perl5/site_perl/5.005/i386-linux/usr/lib/perl5/site_perl/5.005.
Notice the .
(current directory) is the last directory in the list.
Now let's load the module strict.pm
and see the contents of %INC
: 打印%INC
% perl -e 'use strict; print map {"$_ => $INC{$_}\n"} keys %INC'strict.pm => /usr/lib/perl5/5.00503/strict.pm
Now let's create the simplest module in /tmp/test.pm
:
test.pm-------1;
It does nothing, but returns a true value when loaded. Now let's load it in different ways:
% cd /tmp% perl -e 'use test; print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => test.pm
% cd /tmp% perl -e 'BEGIN{push @INC, "/tmp"} use test; \print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => test.pm
% cd /% perl -e 'BEGIN{push @INC, "/tmp"} use test; \print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => /tmp/test.pm
% cd /tmp% perl -e 'BEGIN{unshift @INC, "/tmp"} use test; \print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => /tmp/test.pm
BEGIN{unshift @INC, "/tmp"}
can be replaced with the more elegant:
use lib "/tmp";
Which is almost equivalent to our BEGIN
block and is the recommended approach.
For the sake of completeness, I'll present the FindBin
module anyway.
load.pl-------#!/usr/bin/perluse FindBin ();use lib "$FindBin::Bin";use test;print "test.pm => $INC{'test.pm'}\n";
% /tmp/load.pltest.pm => /tmp/test.pm
This is just like use lib
except that no hard coded path is required.
You can use this workaround to make it work under mod_perl.
do 'FindBin.pm';unshift @INC, "$FindBin::Bin";require test;#maybe test::import( ... ) here if need to import stuff
查看某个模块是在哪个lib路径下被登录的(一般是写在use 之后的某个要测试的地方)
print($INC{"Cwd.pm"}, "\n"); 如果有则输出对应文件所在的位置。如 C:/Perl/lib/Cwd.pm
print($INC{"File/Basename.pm"}, "\n"); 这边要找的是use File::Basename所使用的模块位置 如 C:/Perl/lib/File/Basename.pm
print($INC{"Basename.pm"}, "\n"); 如果还没登录、文件路径不对,则不输出任何东西
Modules, Libraries and Program Files
Before we proceed, let's define what we mean by module, library and program file.
- Libraries(这类可以归为一般perl和module中去。如果要用,可以作为.h来用)
These are files which contain Perl subroutines and other code.
When these are used to break up a large program into manageable chunks they don't generally include a package declaration; when they are used as subroutine libraries they often do have a package declaration.
如果真的作为一个module用,则Their last statement returns true, a simple
1;
statement ensures that.They can be named in any way desired, but generally their extension is .pl.
Examples:
config.pl----------# No package so defaults to main::$dir = "/home/httpd/cgi-bin";$cgi = "/cgi-bin";1;mysubs.pl----------# No package so defaults to main::sub print_header{print "Content-type: text/plain\r\n\r\n";}1;web.pl------------package web ;# Call like this: web::print_with_class('loud',"Don't shout!");sub print_with_class{my ( $class, $text ) = @_ ;print qq{<span class="$class">$text</span>};}1;
- Modules
A file which contains perl subroutines and other code.
It generally declares a package name at the beginning of it.
Modules are generally used either as function libraries (which .pl files are still but less commonly used for), or as object libraries where a module is used to define a class and its methods.
Its last statement returns true.
The naming convention requires it to have a .pm extension.
Example:
MyModule.pm-----------package My::Module;$My::Module::VERSION = 0.01;sub new{ return bless {}, shift;}END { print "Quitting\n"}1;
- Program Files
Many Perl programs exist as a single file. Under Linux and other Unix-like operating systems the file often has no suffix since the operating system can determine that it is a perl script from the first line (shebang line) or if it's Apache that executes the code, there is a variety of ways to tell how and when the file should be executed. Under Windows a suffix is normally used, for example
.pl
or.plx
.The program file will normally
require()
any libraries anduse()
any modules it requires for execution.It will contain Perl code but won't usually have any package names.
Its last statement may return anything or nothing.
require()
require "/home/httpd/perl/mylibs.pl";
require "mylibs.pl";
require My::Module;
require "My/Module.pm";
require.pl----------#!/usr/bin/perl -weval { require "/file/that/does/not/exists"};if ($@) {print "Failed to load, because : $@"}print "\nHello\n";
% ./require.plFailed to load, because : Can't locate /file/that/does/not/exists in@INC (@INC contains: /usr/lib/perl5/5.00503/i386-linux/usr/lib/perl5/5.00503 /usr/lib/perl5/site_perl/5.005/i386-linux/usr/lib/perl5/site_perl/5.005 .) at require.pl line 3.Hello
If we remove the eval() part and try again:
require.pl----------#!/usr/bin/perl -wrequire "/file/that/does/not/exists";print "\nHello\n";% ./require1.plCan't locate /file/that/does/not/exists in @INC (@INC contains:/usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503/usr/lib/perl5/site_perl/5.005/i386-linux/usr/lib/perl5/site_perl/5.005 .) at require1.pl line 3.
The program just die()s in the last example, which is what you want in most cases.
use()
use MyCode
use "MyCode.pm"
use() is exactly equivalent to:
BEGIN { require Module; Module->import(LIST); }
use MyModule;BEGIN {require MyModule; MyModule->import; }use MyModule qw(foo bar);BEGIN {require MyModule; MyModule->import("foo","bar"); }use MyModule ();BEGIN {require MyModule; }
The second pair exports only the tags passed as arguments.
The third pair describes the case where the caller does not want any symbols to be imported.
转载于:https://www.cnblogs.com/spriteflk/p/5737852.html
Perl 之 use(), require(), do(), %INC and @INC相关推荐
- perl use和require的用法
本文和大家重点学习一下Perl use和require用法对比,这两个函数都是一个意思,加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域 ...
- Perl中use和require用法对比
本文和大家重点学习一下Perl use和require用法对比,这两个函数都是一个意思,加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域 ...
- 编辑神器VIM下安装zencoding
各大编辑器使用不如人意,notepad++,多人推荐,但zencoding使用太小家气.对我而言,其它的如ue启动太慢,等等原因. 不得不用神器: vim下载, http://www.vim.org/ ...
- 改写了一个shell写的cdn节点测试代码
本文受http://wangxiaoyu.blog.51cto.com/922065/514570这篇文章的感染,就模拟用perl写了一个,供参考.呵呵... 本脚本运行环境: badboy@www: ...
- java的import和python的import对比_import导入的是什么
Q1:from import导入出错,单独导入则正确.. 您好, from UserDict import UserDict 它与你所熟知的 import module 语法很相似,但是有一个重要的区 ...
- MySQL中文参考手册--1.MySQL的一般信息
MySQL中文参考手册--1.MySQL的一般信息 0 译者序 MySQL是一个精巧的SQL数据库管理系统,虽然它不是开放源代码的产品,但在某些情况下你可以自由使用.由于它的强大功能.灵活性.丰富的应 ...
- perl-编译方法(转)
perl-全面编译(转) 文章分类:综合技术 资迅 论坛 教程 杂志 SNS 搜索 - Linux教程 Linux Linux编程 C/C++编程 Python编程 Perl编程 PH ...
- perl-全面编译(转)
资迅 论坛 教程 杂志 SNS 搜索 - Linux教程 Linux Linux编程 C/C++编程 Python编程 Perl编程 PHP编程 shell 编程技术 kerne ...
- Perl中use、require的用法和区别
2019独角兽企业重金招聘Python工程师标准>>> Exporter Module usage ...
最新文章
- XAMPP维基百科,自由的百科全书
- 品牌管理-统一异常处理
- Cookie中不能有空格_PHP 使用cookie
- 事物 @Transactional
- Windows静态库和动态库的调用方法汇总
- Servlet 版本与web.xml配置
- python删除指定路径文件_基于python实现删除指定文件类型
- [Tailwind] Control What Variations are Generated for Each Utility Class Module in Tailwind
- 打开客户端出现 sorry this application cannot run under a virtual machine错误
- 适合智能电视的K歌神器,纯麦U7PRO麦克风体验
- php 上传图片 裁剪,thinkphp_图片上传裁剪功能
- Matlab高光谱遥感、数据处理与混合像元分解实践技术应用
- 文笔很差系列2 - 真实的世界
- 电脑有网却打不开网页(能ping通)
- 计算机网络一到六章知识点
- [BI] 搭建开源BI平台Superset,连接Oracle数据库
- 图书馆:模拟一天的借阅过程
- MySQL8.0导入数据
- 我的北漂生活:我来北京一个月了
- 支付宝手机网页支付java demo调试问题和解决办法
热门文章
- [Qt教程] 第15篇 2D绘图(五)绘制图片
- 广东省计算机大赛设计什么时候,2017年广东省大学生计算机设计大赛
- .so是什么文件_安卓 so 文件解析详解
- C++11学习笔记-----获取异步操作执行结果
- 每天一道LeetCode-----将数组/链表后k个元素移动到前面
- 学习笔记-----C++模板类中友元函数重载输出运算符时提示无法解析的外部符号解决方案
- java ajax翻页_分页 工具类 前后台代码 Java JavaScript (ajax) 实现 讲解
- Hbase(1)——基础语句(1)
- pybind11简单使用
- 4键键盘(Leetcode651)