日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Perl中use、require的用法和区别

發(fā)布時間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Perl中use、require的用法和区别 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

Exporter Module


  • usage ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??implements an?import?method which allows a module to export?functions and variables to its users' namespaces.
  • package YourModule; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(munge frobnicate); # symbols to export on request package YourModule; use Exporter 'import'; # gives you Exporter's import() method directly @EXPORT_OK = qw(munge frobnicate); # symbols to export on request

    Perl automatically calls the?import?method when processing a?use?statement for a module.

    use YourModule qw(frobnicate); # import listed symbols frobnicate ($left, $right) # calls YourModule::frobnicate

    @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS

    ?@ISA:Within each package a special array called @ISA tells Perl where else to look for a method if it can't find the method in that package. This is how Perl implements inheritance.


    Each element of the @ISA array is just the name of another package that happens to be used as a class. The packages are recursively searched (depth first) for missing methods, in the order that packages are mentioned in @ISA.


    This means that if you have two different packages (say, Mom and Dad) in a class's @ISA, Perl would first look for missing methods in Mom and all of her ancestor classes before going on to search through Dad and his ancestors. Classes accessible through @ISA are known as base classes of the current class, which is itself called the derived class.


    @EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

    @EXPORT = qw(...);?
    Symbols to export by default


    @EXPORT_OK this array stores the subroutins to be exported only on request.
    @EXPORT_OK = qw(...);?
    Symbols to export on request.


    %EXPORT_TAGS = (tag => [...]);?
    Define names for sets of symbols
    Since the symbols listed within %EXPORT_TAGS must also appear in either @EXPORT or EXPORT_OK,
    two utility functions are provided that allow you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
    %EXPORT_TAGS = (Bactrian => [qw(aa bb cc)], Dromedary => [qw(aa cc dd)]);
    Exporter::export_tags('Bactrian'); # add aa, bb and cc to @EXPORT
    Exporter::export_ok_tags('Dromedary'); # add aa, cc and dd to @EXPORT_OK

    How to EXPORT

    The arrays? @EXPORT ?and? @EXPORT_OK? ?in a module hold lists of symbols that are going to be exported into the users name space by default, or which they can request to be exported, respectively. The symbols can represent functions, scalars, arrays, hashes, or typeglobs.

    How to import

    In other files which wish to use your module there are three basic ways for them to load your module and import its symbols:

  • use YourModule; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?This imports all the symbols from YourModule's?@EXPORT?into the namespace of the?use?statement.
  • use YourModule (); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??This causes perl to load your module but does not import any symbols.
  • use YourModule qw(...); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? This imports only the symbols listed by the caller into their namespace. All listed symbols must be in your?@EXPORT?or?@EXPORT_OK, else an error occurs. The advanced export features of Exporter are accessed like this, but with list entries that are syntactically distinct from symbol names.

  • @INC

    @INC?is a special Perl variable which is the equivalent of the shell's?PATH?variable. Whereas?PATH?contains a list of directories to search for executables,?@INC?contains a list of directories from which Perl modules and libraries can be loaded.

    When you use(), require() or do() a filename or a module, Perl gets a list of directories from the?@INC?variable and searches them for the file it was requested to load. If the file that you want to load is not located in one of the listed directories, you have to tell Perl where to find the file. You can either provide a path relative to one of the directories in?@INC, or you can provide the full path to the file.

    %INC

    %INC?is another special Perl variable that is used to cache the names of the files and the modules that were successfully loaded and compiled by use(), require() or do() statements.Before attempting to load a file or a module with use() or require(), Perl checks whether it's already in the?%INC?hash. If it's there, the loading and therefore the compilation are not performed at all. Otherwise the file is loaded into memory and an attempt is made to compile it.do() does unconditional loading--no lookup in the?%INC?hash is made.

    If the file is successfully loaded and compiled, a new key-value pair is added to?%INC. The key is the name of the file or module as it was passed to the one of the three functions we have just mentioned, and if it was found in any of the?@INC?directories except?"."?the value is the full path to it in the file 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.

    % 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

    Since the file was found relative to?.?(the current directory), the relative path is inserted as the value. If we alter?@INC, by adding?/tmp?to the end:

    % cd /tmp% perl -e 'BEGIN{push @INC, "/tmp"} use test; \print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => test.pm


    require

    require() reads a file containing Perl code and compiles it.Before attempting to load the file it looks up the argument in?%INC?to see whether it has already been loaded. If it has, require() just returns without doing a thing. Otherwise an attempt will be made to load and compile the file.

    This differs from use in that included files effectively become additional text for the current script. Functions, variables, and other objects are not imported into the current name space, so if the specified file includes a package definition, then objects will require fully qualified names.

    The specified module is searched for in the directories defined in @INC, looking for a file with the specified name and an extension of .pm.

    1、require用于載入module或perl程序(.pm后綴可以省略,但.pl必須有)
    2、require在運(yùn)行時載入(驗(yàn)證)module

    require htolib::test
    這條語句相當(dāng)于:require “htolib/test.pm”

    如果使用require 'filename'或者require "filename"來包含文件的話,使用方法和do完全近似;

  • 如果不加單引號或者雙引號的話,后面的Module將被解析為Perl的模塊即.pm文件,然后根據(jù)@INC Array中搜索Module.pm文件。首先在當(dāng)前目錄下搜索Module.pm的文件(用戶自定義的),如果找不到再去Perl的 (@INC contains: C:/Perl/site/lib C:/Perl/lib .)尋找。
  • use


    use(), just like require(), loads and compiles files containing Perl code, but it works with?modules?only and is executed at compile time.

    The only way to pass a module to load is by its module name and not its filename. If the module is located in?MyCode.pm, the correct way to use() it is:

    use MyCode

    and not:

    use "MyCode.pm"

    use() translates the passed argument into a file name replacing?::?with the operating system's path separator (normally?/) and appending?.pm?at the end. So?My::Module?becomes?My/Module.pm.

    use() is exactly equivalent to:

    BEGIN { require Module; Module->import(LIST); }

    Internally it calls require() to do the loading and compilation chores. When require() finishes its job, import() is called unless?()?is the second argument. The following pairs are equivalent:

    use MyModule;BEGIN {require MyModule; MyModule->import; }use MyModule qw(foo bar);BEGIN {require MyModule; MyModule->import("foo","bar"); }use MyModule ();BEGIN {require MyModule; }

    do

    While do() behaves almost identically to require(), it reloads the file unconditionally. It doesn't check?%INC?to see whether the file was already loaded.

    If do() cannot read the file, it returns?undef?and sets?$!?to report the error. If do() can read the file but cannot compile it, it returns?undef?and puts an error message in?$@. If the file is successfully compiled, do() returns the value of the last expression evaluated.

    轉(zhuǎn)載于:https://my.oschina.net/u/347414/blog/267747

    總結(jié)

    以上是生活随笔為你收集整理的Perl中use、require的用法和区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 2022天天操 | 最近中文字幕在线观看 | 欧美色插 | 9.1成人免费看片 | 一女双乳被两男吸视频 | 国产精品毛片久久久久久久av | 亚洲色成人网站www永久四虎 | aaaa免费视频 | 久久久久国产精品 | 久视频在线 | 免费黄色三级 | 久久久久久久久精 | 精品麻豆一区二区 | 日本国产精品一区 | 国产乱码精品一区二区 | 福利av在线 | 黄色资源在线播放 | 国产中文久久 | 日韩成人av网 | av片毛片| 国产精品美女久久久久av超清 | 香蕉狠狠爱视频 | 无码av免费毛片一区二区 | 美女露胸露尿口 | 黑人一区二区三区四区五区 | 亚洲欧洲日产av | 日日日操操操 | 午夜亚洲天堂 | 尤物视频在线观看免费 | 91在线精品秘密一区二区 | 天天色天天射天天操 | 免费无码国产v片在线观看 三级全黄做爰在线观看 | 叶全真三级 | 亚洲午夜小视频 | 久久99中文字幕 | 羞羞的软件| 欧美一级淫片 | 岛国精品一区二区三区 | 超碰97国产| 日本精品久久久久久久 | 久久久久久久99 | 亚洲无码精品在线观看 | 先锋av资源网 | 黄片毛片在线看 | 成人欧美一区二区三区黑人 | 成人区人妻精品一区二区网站 | 久久夜夜夜 | 久久精品国产99久久不卡 | 进去里视频在线观看 | 视频在线91| 波多野结衣一本 | 精品久久久噜噜噜久久久 | 久久免费国产精品 | 美女啪啪免费视频 | 亚洲精品久久久久av无码 | 色吧久久 | 黄色网页在线 | 少妇被躁爽到高潮无码文 | 国产美女视频一区二区 | 欧美乱大交xxxxx潮喷 | 精品人妻无码中文字幕18禁 | 中文字幕乱码人妻一区二区三区 | 奇米四色777 | 午夜影院0606 | 三年大全国语中文版免费播放 | 乱中年女人伦 | 久久久久高清 | 免费网站成人 | 欧美人体一区二区 | 欧美成人三级在线 | 国产精品成人免费 | 黄色在线观看免费 | 欧美久久99| 欧美成人aaa片一区国产精品 | 强行无套内谢大学生初次 | 免看黄大片aa | 国产一二区在线观看 | 国产三级在线播放 | 光棍影院手机版在线观看免费 | 亚洲夜夜操 | 国产av无码专区亚洲av麻豆 | 女女互磨互喷水高潮les呻吟 | 久久影院午夜理论片无码 | 国产欧美a | 性欧美hd调教 | 激情黄色小说视频 | 丰满饥渴老女人hd | 中文字幕第一页av | 蜜臀久久久久久999 大陆熟妇丰满多毛xxxⅹ | 久草视频在线资源 | 波多野结衣啪啪 | 夫妻精品 | 亚洲视频免费在线 | jizz韩国| 午夜久久久 | av在观看 | 日韩成人在线视频观看 | 三级国产三级在线 | 爱的色放韩国电影 |