日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。