⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlmod.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 3 页
字号:
\&  BEGIN { print " 2.   So this line comes out second.\en" }\&  UNITCHECK {\&   print " 3. UNITCHECK blocks run LIFO after each file is compiled.\en"\&  }\&  INIT { print  " 9.   You\*(Aqll see the difference right away.\en" }\&\&  print         "13.   It merely _looks_ like it should be confusing.\en";\&\&  _\|_END_\|_.Ve.Sh "Perl Classes".IX Xref "class @ISA".IX Subsection "Perl Classes"There is no special class syntax in Perl, but a package may actas a class if it provides subroutines to act as methods.  Such apackage may also derive some of its methods from another class (package)by listing the other package name(s) in its global \f(CW@ISA\fR array (whichmust be a package global, not a lexical)..PPFor more on this, see perltoot and perlobj..Sh "Perl Modules".IX Xref "module".IX Subsection "Perl Modules"A module is just a set of related functions in a library file, i.e.,a Perl package with the same name as the file.  It is specificallydesigned to be reusable by other modules or programs.  It may do thisby providing a mechanism for exporting some of its symbols into thesymbol table of any package using it, or it may function as a classdefinition and make its semantics available implicitly throughmethod calls on the class and its objects, without explicitlyexporting anything.  Or it can do a little of both..PPFor example, to start a traditional, non-OO module called Some::Module,create a file called \fISome/Module.pm\fR and start with this template:.PP.Vb 1\&    package Some::Module;  # assumes Some/Module.pm\&\&    use strict;\&    use warnings;\&\&    BEGIN {\&        use Exporter   ();\&        our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);\&\&        # set the version for version checking\&        $VERSION     = 1.00;\&        # if using RCS/CVS, this may be preferred\&        $VERSION = sprintf "%d.%03d", q$Revision: 1.1 $ =~ /(\ed+)/g;\&\&        @ISA         = qw(Exporter);\&        @EXPORT      = qw(&func1 &func2 &func4);\&        %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],\&\&        # your exported package globals go here,\&        # as well as any optionally exported functions\&        @EXPORT_OK   = qw($Var1 %Hashit &func3);\&    }\&    our @EXPORT_OK;\&\&    # exported package globals go here\&    our $Var1;\&    our %Hashit;\&\&    # non\-exported package globals go here\&    our @more;\&    our $stuff;\&\&    # initialize package globals, first exported ones\&    $Var1   = \*(Aq\*(Aq;\&    %Hashit = ();\&\&    # then the others (which are still accessible as $Some::Module::stuff)\&    $stuff  = \*(Aq\*(Aq;\&    @more   = ();\&\&    # all file\-scoped lexicals must be created before\&    # the functions below that use them.\&\&    # file\-private lexicals go here\&    my $priv_var    = \*(Aq\*(Aq;\&    my %secret_hash = ();\&\&    # here\*(Aqs a file\-private function as a closure,\&    # callable as &$priv_func;  it cannot be prototyped.\&    my $priv_func = sub {\&        # stuff goes here.\&    };\&\&    # make all your functions, whether exported or not;\&    # remember to put something interesting in the {} stubs\&    sub func1      {}    # no prototype\&    sub func2()    {}    # proto\*(Aqd void\&    sub func3($$)  {}    # proto\*(Aqd to 2 scalars\&\&    # this one isn\*(Aqt exported, but could be called!\&    sub func4(\e%)  {}    # proto\*(Aqd to 1 hash ref\&\&    END { }       # module clean\-up code here (global destructor)\&\&    ## YOUR CODE GOES HERE\&\&    1;  # don\*(Aqt forget to return a true value from the file.Ve.PPThen go on to declare and use your variables in functions withoutany qualifications.  See Exporter and the perlmodlib fordetails on mechanics and style issues in module creation..PPPerl modules are included into your program by saying.PP.Vb 1\&    use Module;.Ve.PPor.PP.Vb 1\&    use Module LIST;.Ve.PPThis is exactly equivalent to.PP.Vb 1\&    BEGIN { require Module; import Module; }.Ve.PPor.PP.Vb 1\&    BEGIN { require Module; import Module LIST; }.Ve.PPAs a special case.PP.Vb 1\&    use Module ();.Ve.PPis exactly equivalent to.PP.Vb 1\&    BEGIN { require Module; }.Ve.PPAll Perl module files have the extension \fI.pm\fR.  The \f(CW\*(C`use\*(C'\fR operatorassumes this so you don't have to spell out "\fIModule.pm\fR" in quotes.This also helps to differentiate new modules from old \fI.pl\fR and\&\fI.ph\fR files.  Module names are also capitalized unless they'refunctioning as pragmas; pragmas are in effect compiler directives,and are sometimes called \*(L"pragmatic modules\*(R" (or even \*(L"pragmata\*(R"if you're a classicist)..PPThe two statements:.PP.Vb 2\&    require SomeModule;\&    require "SomeModule.pm";.Ve.PPdiffer from each other in two ways.  In the first case, any doublecolons in the module name, such as \f(CW\*(C`Some::Module\*(C'\fR, are translatedinto your system's directory separator, usually \*(L"/\*(R".   The secondcase does not, and would have to be specified literally.  The otherdifference is that seeing the first \f(CW\*(C`require\*(C'\fR clues in the compilerthat uses of indirect object notation involving \*(L"SomeModule\*(R", asin \f(CW\*(C`$ob = purge SomeModule\*(C'\fR, are method calls, not function calls.(Yes, this really can make a difference.).PPBecause the \f(CW\*(C`use\*(C'\fR statement implies a \f(CW\*(C`BEGIN\*(C'\fR block, the importingof semantics happens as soon as the \f(CW\*(C`use\*(C'\fR statement is compiled,before the rest of the file is compiled.  This is how it is ableto function as a pragma mechanism, and also how modules are able todeclare subroutines that are then visible as list or unary operators forthe rest of the current file.  This will not work if you use \f(CW\*(C`require\*(C'\fRinstead of \f(CW\*(C`use\*(C'\fR.  With \f(CW\*(C`require\*(C'\fR you can get into this problem:.PP.Vb 2\&    require Cwd;                # make Cwd:: accessible\&    $here = Cwd::getcwd();\&\&    use Cwd;                    # import names from Cwd::\&    $here = getcwd();\&\&    require Cwd;                # make Cwd:: accessible\&    $here = getcwd();           # oops! no main::getcwd().Ve.PPIn general, \f(CW\*(C`use Module ()\*(C'\fR is recommended over \f(CW\*(C`require Module\*(C'\fR,because it determines module availability at compile time, not in themiddle of your program's execution.  An exception would be if two moduleseach tried to \f(CW\*(C`use\*(C'\fR each other, and each also called a function fromthat other module.  In that case, it's easy to use \f(CW\*(C`require\*(C'\fR instead..PPPerl packages may be nested inside other package names, so we can havepackage names containing \f(CW\*(C`::\*(C'\fR.  But if we used that package namedirectly as a filename it would make for unwieldy or impossiblefilenames on some systems.  Therefore, if a module's name is, say,\&\f(CW\*(C`Text::Soundex\*(C'\fR, then its definition is actually found in the libraryfile \fIText/Soundex.pm\fR..PPPerl modules always have a \fI.pm\fR file, but there may also bedynamically linked executables (often ending in \fI.so\fR) or autoloadedsubroutine definitions (often ending in \fI.al\fR) associated with themodule.  If so, these will be entirely transparent to the user ofthe module.  It is the responsibility of the \fI.pm\fR file to load(or arrange to autoload) any additional functionality.  For example,although the \s-1POSIX\s0 module happens to do both dynamic loading andautoloading, the user can say just \f(CW\*(C`use POSIX\*(C'\fR to get it all..Sh "Making your module threadsafe".IX Xref "threadsafe thread safe module, threadsafe module, thread safe CLONE CLONE_SKIP thread threads ithread".IX Subsection "Making your module threadsafe"Since 5.6.0, Perl has had support for a new type of threads calledinterpreter threads (ithreads). These threads can be used explicitlyand implicitly..PPIthreads work by cloning the data tree so that no data is sharedbetween different threads. These threads can be used by using the \f(CW\*(C`threads\*(C'\fRmodule or by doing \fIfork()\fR on win32 (fake \fIfork()\fR support). When athread is cloned all Perl data is cloned, however non-Perl data cannotbe cloned automatically.  Perl after 5.7.2 has support for the \f(CW\*(C`CLONE\*(C'\fRspecial subroutine.  In \f(CW\*(C`CLONE\*(C'\fR you can do whateveryou need to do,like for example handle the cloning of non-Perl data, if necessary.\&\f(CW\*(C`CLONE\*(C'\fR will be called once as a class method for every package that has itdefined (or inherits it).  It will be called in the context of the new thread,so all modifications are made in the new area.  Currently \s-1CLONE\s0 is called withno parameters other than the invocant package name, but code should not assumethat this will remain unchanged, as it is likely that in future extra parameterswill be passed in to give more information about the state of cloning..PPIf you want to \s-1CLONE\s0 all objects you will need to keep track of them perpackage. This is simply done using a hash and \fIScalar::Util::weaken()\fR..PPPerl after 5.8.7 has support for the \f(CW\*(C`CLONE_SKIP\*(C'\fR special subroutine.Like \f(CW\*(C`CLONE\*(C'\fR, \f(CW\*(C`CLONE_SKIP\*(C'\fR is called once per package; however, it iscalled just before cloning starts, and in the context of the parentthread. If it returns a true value, then no objects of that class willbe cloned; or rather, they will be copied as unblessed, undef values.For example: if in the parent there are two references to a single blessedhash, then in the child there will be two references to a single undefinedscalar value instead.This provides a simple mechanism for making a module threadsafe; just add\&\f(CW\*(C`sub CLONE_SKIP { 1 }\*(C'\fR at the top of the class, and \f(CW\*(C`DESTROY()\*(C'\fR will benow only be called once per object. Of course, if the child thread needsto make use of the objects, then a more sophisticated approach isneeded..PPLike \f(CW\*(C`CLONE\*(C'\fR, \f(CW\*(C`CLONE_SKIP\*(C'\fR is currently called with no parameters otherthan the invocant package name, although that may change. Similarly, toallow for future expansion, the return value should be a single \f(CW0\fR or\&\f(CW1\fR value..SH "SEE ALSO".IX Header "SEE ALSO"See perlmodlib for general style issues related to building Perlmodules and classes, as well as descriptions of the standard libraryand \s-1CPAN\s0, Exporter for how Perl's standard import/export mechanismworks, perltoot and perltooc for an in-depth tutorial oncreating classes, perlobj for a hard-core reference document onobjects, perlsub for an explanation of functions and scoping,and perlxstut and perlguts for more information on writingextension modules.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -