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

📄 perlfaq3.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 4 页
字号:
the current version of vi out of Berkeley, which incidentally can be builtwith an embedded Perl interpreter\*(--see http://www.cpan.org/src/misc/ ..Sh "Where can I get perl-mode for emacs?".IX Subsection "Where can I get perl-mode for emacs?"Since Emacs version 19 patchlevel 22 or so, there have been both aperl\-mode.el and support for the Perl debugger built in.  These shouldcome with the standard Emacs 19 distribution..PPIn the Perl source directory, you'll find a directory called \*(L"emacs\*(R",which contains a cperl-mode that color-codes keywords, providescontext-sensitive help, and other nifty things..PPNote that the perl-mode of emacs will have fits with \f(CW"main\*(Aqfoo"\fR(single quote), and mess up the indentation and highlighting.  Youare probably using \f(CW"main::foo"\fR in new Perl code anyway, so thisshouldn't be an issue..Sh "How can I use curses with Perl?".IX Subsection "How can I use curses with Perl?"The Curses module from \s-1CPAN\s0 provides a dynamically loadable objectmodule interface to a curses library.  A small demo can be found at thedirectory http://www.cpan.org/authors/Tom_Christiansen/scripts/rep.gz ;this program repeats a command and updates the screen as needed, rendering\&\fBrep ps axu\fR similar to \fBtop\fR..Sh "How can I write a \s-1GUI\s0 (X, Tk, Gtk, etc.) in Perl?".IX Xref "GUI Tk Wx WxWidgets Gtk Gtk2 CamelBones Qt".IX Subsection "How can I write a GUI (X, Tk, Gtk, etc.) in Perl?"(contributed by Ben Morrow).PPThere are a number of modules which let you write GUIs in Perl. Most\&\s-1GUI\s0 toolkits have a perl interface: an incomplete list follows..IP "Tk" 4.IX Item "Tk"This works under Unix and Windows, and the current version doesn'tlook half as bad under Windows as it used to. Some of the gui elementsstill don't 'feel' quite right, though. The interface is very naturaland 'perlish', making it easy to use in small scripts that just need asimple gui. It hasn't been updated in a while..IP "Wx" 4.IX Item "Wx"This is a Perl binding for the cross-platform wxWidgets toolkit <http://www.wxwidgets.org>. It works under Unix, Win32 and Mac \s-1OS\s0 X,using native widgets (Gtk under Unix). The interface follows the \*(C+interface closely, but the documentation is a little sparse for someonewho doesn't know the library, mostly just referring you to the \*(C+documentation..IP "Gtk and Gtk2" 4.IX Item "Gtk and Gtk2"These are Perl bindings for the Gtk toolkit <http://www.gtk.org>. Theinterface changed significantly between versions 1 and 2 so they haveseparate Perl modules. It runs under Unix, Win32 and Mac \s-1OS\s0 X (currentlyit requires an X server on Mac \s-1OS\s0, but a 'native' port is underway), andthe widgets look the same on every plaform: i.e., they don't match thenative widgets. As with Wx, the Perl bindings follow the C \s-1API\s0 closely,and the documentation requires you to read the C documentation tounderstand it..IP "Win32::GUI" 4.IX Item "Win32::GUI"This provides access to most of the Win32 \s-1GUI\s0 widgets from Perl.Obviously, it only runs under Win32, and uses native widgets. The Perlinterface doesn't really follow the C interface: it's been made morePerlish, and the documentation is pretty good. More advanced stuff mayrequire familiarity with the C Win32 APIs, or reference to \s-1MSDN\s0..IP "CamelBones" 4.IX Item "CamelBones"CamelBones <http://camelbones.sourceforge.net> is a Perl interface toMac \s-1OS\s0 X's Cocoa \s-1GUI\s0 toolkit, and as such can be used to produce nativeGUIs on Mac \s-1OS\s0 X. It's not on \s-1CPAN\s0, as it requires frameworks that\&\s-1CPAN\s0.pm doesn't know how to install, but installation is via thestandard \s-1OSX\s0 package installer. The Perl \s-1API\s0 is, again, very close tothe ObjC \s-1API\s0 it's wrapping, and the documentation just tells you how totranslate from one to the other..IP "Qt" 4.IX Item "Qt"There is a Perl interface to TrollTech's Qt toolkit, but it does notappear to be maintained..IP "Athena" 4.IX Item "Athena"Sx is an interface to the Athena widget set which comes with X, butagain it appears not to be much used nowadays..Sh "How can I make my Perl program run faster?".IX Subsection "How can I make my Perl program run faster?"The best way to do this is to come up with a better algorithm.  Thiscan often make a dramatic difference.  Jon Bentley's book\&\fIProgramming Pearls\fR (that's not a misspelling!)  has some good tipson optimization, too.  Advice on benchmarking boils down to: benchmarkand profile to make sure you're optimizing the right part, look forbetter algorithms instead of microtuning your code, and when all elsefails consider just buying faster hardware.  You will probably want toread the answer to the earlier question \*(L"How do I profile my Perlprograms?\*(R" if you haven't done so already..PPA different approach is to autoload seldom-used Perl code.  See theAutoSplit and AutoLoader modules in the standard distribution forthat.  Or you could locate the bottleneck and think about writing justthat part in C, the way we used to take bottlenecks in C code andwrite them in assembler.  Similar to rewriting in C, modules that havecritical sections can be written in C (for instance, the \s-1PDL\s0 modulefrom \s-1CPAN\s0)..PPIf you're currently linking your perl executable to a shared\&\fIlibc.so\fR, you can often gain a 10\-25% performance benefit byrebuilding it to link with a static libc.a instead.  This will make abigger perl executable, but your Perl programs (and programmers) maythank you for it.  See the \fI\s-1INSTALL\s0\fR file in the source distributionfor more information..PPThe undump program was an ancient attempt to speed up Perl program bystoring the already-compiled form to disk.  This is no longer a viableoption, as it only worked on a few architectures, and wasn't a goodsolution anyway..Sh "How can I make my Perl program take less memory?".IX Subsection "How can I make my Perl program take less memory?"When it comes to time-space tradeoffs, Perl nearly always prefers tothrow memory at a problem.  Scalars in Perl use more memory thanstrings in C, arrays take more than that, and hashes use even more.  Whilethere's still a lot to be done, recent releases have been addressingthese issues.  For example, as of 5.004, duplicate hash keys areshared amongst all hashes using them, so require no reallocation..PPIn some cases, using \fIsubstr()\fR or \fIvec()\fR to simulate arrays can behighly beneficial.  For example, an array of a thousand booleans willtake at least 20,000 bytes of space, but it can be turned into one125\-byte bit vector\*(--a considerable memory savings.  The standardTie::SubstrHash module can also help for certain types of datastructure.  If you're working with specialist data structures(matrices, for instance) modules that implement these in C may useless memory than equivalent Perl modules..PPAnother thing to try is learning whether your Perl was compiled withthe system malloc or with Perl's builtin malloc.  Whichever one itis, try using the other one and see whether this makes a difference.Information about malloc is in the \fI\s-1INSTALL\s0\fR file in the sourcedistribution.  You can find out whether you are using perl's malloc bytyping \f(CW\*(C`perl \-V:usemymalloc\*(C'\fR..PPOf course, the best way to save memory is to not do anything to wasteit in the first place. Good programming practices can go a long waytoward this:.IP "\(bu" 4Don't slurp!.SpDon't read an entire file into memory if you can process it lineby line. Or more concretely, use a loop like this:.Sp.Vb 6\&        #\&        # Good Idea\&        #\&        while (<FILE>) {\&           # ...\&        }.Ve.Spinstead of this:.Sp.Vb 7\&        #\&        # Bad Idea\&        #\&        @data = <FILE>;\&        foreach (@data) {\&            # ...\&        }.Ve.SpWhen the files you're processing are small, it doesn't much matter whichway you do it, but it makes a huge difference when they start gettinglarger..IP "\(bu" 4Use map and grep selectively.SpRemember that both map and grep expect a \s-1LIST\s0 argument, so doing this:.Sp.Vb 1\&        @wanted = grep {/pattern/} <FILE>;.Ve.Spwill cause the entire file to be slurped. For large files, it's betterto loop:.Sp.Vb 3\&        while (<FILE>) {\&                push(@wanted, $_) if /pattern/;\&        }.Ve.IP "\(bu" 4Avoid unnecessary quotes and stringification.SpDon't quote large strings unless absolutely necessary:.Sp.Vb 1\&        my $copy = "$large_string";.Ve.Spmakes 2 copies of \f(CW$large_string\fR (one for \f(CW$copy\fR and another for thequotes), whereas.Sp.Vb 1\&        my $copy = $large_string;.Ve.Sponly makes one copy..SpDitto for stringifying large arrays:.Sp.Vb 4\&        {\&                local $, = "\en";\&                print @big_array;\&        }.Ve.Spis much more memory-efficient than either.Sp.Vb 1\&        print join "\en", @big_array;.Ve.Spor.Sp.Vb 4\&        {\&                local $" = "\en";\&                print "@big_array";\&        }.Ve.IP "\(bu" 4Pass by reference.SpPass arrays and hashes by reference, not by value. For one thing, it'sthe only way to pass multiple lists or hashes (or both) in a singlecall/return. It also avoids creating a copy of all the contents. Thisrequires some judgement, however, because any changes will be propagatedback to the original data. If you really want to mangle (er, modify) acopy, you'll have to sacrifice the memory needed to make one..IP "\(bu" 4Tie large variables to disk..SpFor \*(L"big\*(R" data stores (i.e. ones that exceed available memory) considerusing one of the \s-1DB\s0 modules to store it on disk instead of in \s-1RAM\s0. Thiswill incur a penalty in access time, but that's probably better thancausing your hard disk to thrash due to massive swapping..Sh "Is it safe to return a reference to local or lexical data?".IX Subsection "Is it safe to return a reference to local or lexical data?"Yes. Perl's garbage collection system takes care of this soeverything works out right..PP.Vb 4\&    sub makeone {\&        my @a = ( 1 .. 10 );\&        return \e@a;\&    }\&\&    for ( 1 .. 10 ) {\&        push @many, makeone();\&    }\&\&    print $many[4][5], "\en";\&\&    print "@many\en";.Ve.Sh "How can I free an array or hash so my program shrinks?".IX Subsection "How can I free an array or hash so my program shrinks?"(contributed by Michael Carman).PPYou usually can't. Memory allocated to lexicals (i.e. \fImy()\fR variables)cannot be reclaimed or reused even if they go out of scope. It isreserved in case the variables come back into scope. Memory allocatedto global variables can be reused (within your program) by using\&\fIundef()\fRing and/or \fIdelete()\fR..PPOn most operating systems, memory allocated to a program can never bereturned to the system. That's why long-running programs sometimes re\-exec themselves. Some operating systems (notably, systems that use\&\fImmap\fR\|(2) for allocating large chunks of memory) can reclaim memory thatis no longer used, but on such systems, perl must be configured andcompiled to use the \s-1OS\s0's malloc, not perl's..PPIn general, memory allocation and de-allocation isn't something you canor should be worrying about much in Perl..PPSee also \*(L"How can I make my Perl program take less memory?\*(R".Sh "How can I make my \s-1CGI\s0 script more efficient?".IX Subsection "How can I make my CGI script more efficient?"Beyond the normal measures described to make general Perl programsfaster or smaller, a \s-1CGI\s0 program has additional issues.  It may be runseveral times per second.  Given that each time it runs it will needto be re-compiled and will often allocate a megabyte or more of systemmemory, this can be a killer.  Compiling into C \fBisn't going to helpyou\fR because the process start-up overhead is where the bottleneck is.

⌨️ 快捷键说明

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