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

📄 perlfaq7.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 4 页
字号:
\&        elsif ("EDIT"  =~ /^\eQ$answer/i) { print "Action is edit\en"  }.Ve.PPA totally different approach is to create a hash of function references..PP.Vb 6\&        my %commands = (\&                "happy" => \e&joy,\&                "sad",  => \e&sullen,\&                "done"  => sub { die "See ya!" },\&                "mad"   => \e&angry,\&        );\&        \&        print "How are you? ";\&        chomp($string = <STDIN>);\&        if ($commands{$string}) {\&                $commands{$string}\->();\&        } else {\&                print "No such command: $string\en";\&        }.Ve.PPNote that starting from version 5.10, Perl has now a native switchstatement. See perlsyn..PPStarting from Perl 5.8, a source filter module, \f(CW\*(C`Switch\*(C'\fR, can also beused to get switch and case. Its use is now discouraged, because it'snot fully compatible with the native switch of Perl 5.10, and because,as it's implemented as a source filter, it doesn't always work as intendedwhen complex syntax is involved..Sh "How can I catch accesses to undefined variables, functions, or methods?".IX Subsection "How can I catch accesses to undefined variables, functions, or methods?"The \s-1AUTOLOAD\s0 method, discussed in \*(L"Autoloading\*(R" in perlsub and\&\*(L"\s-1AUTOLOAD:\s0 Proxy Methods\*(R" in perltoot, lets you capture calls toundefined functions and methods..PPWhen it comes to undefined variables that would trigger a warningunder \f(CW\*(C`use warnings\*(C'\fR, you can promote the warning to an error..PP.Vb 1\&        use warnings FATAL => qw(uninitialized);.Ve.Sh "Why can't a method included in this same file be found?".IX Subsection "Why can't a method included in this same file be found?"Some possible reasons: your inheritance is getting confused, you'vemisspelled the method name, or the object is of the wrong type.  Checkout perltoot for details about any of the above cases.  You mayalso use \f(CW\*(C`print ref($object)\*(C'\fR to find out the class \f(CW$object\fR wasblessed into..PPAnother possible reason for problems is because you've used theindirect object syntax (eg, \f(CW\*(C`find Guru "Samy"\*(C'\fR) on a class namebefore Perl has seen that such a package exists.  It's wisest to makesure your packages are all defined before you start using them, whichwill be taken care of if you use the \f(CW\*(C`use\*(C'\fR statement instead of\&\f(CW\*(C`require\*(C'\fR.  If not, make sure to use arrow notation (eg.,\&\f(CW\*(C`Guru\->find("Samy")\*(C'\fR) instead.  Object notation is explained inperlobj..PPMake sure to read about creating modules in perlmod andthe perils of indirect objects in \*(L"Method Invocation\*(R" in perlobj..Sh "How can I find out my current package?".IX Subsection "How can I find out my current package?"If you're just a random program, you can do this to findout what the currently compiled package is:.PP.Vb 1\&        my $packname = _\|_PACKAGE_\|_;.Ve.PPBut, if you're a method and you want to print an error messagethat includes the kind of object you were called on (which isnot necessarily the same as the one in which you were compiled):.PP.Vb 5\&        sub amethod {\&                my $self  = shift;\&                my $class = ref($self) || $self;\&                warn "called me from a $class object";\&                }.Ve.Sh "How can I comment out a large block of perl code?".IX Subsection "How can I comment out a large block of perl code?"You can use embedded \s-1POD\s0 to discard it.  Enclose the blocks you wantto comment out in \s-1POD\s0 markers.  The <=begin> directive marks a sectionfor a specific formatter.  Use the \f(CW\*(C`comment\*(C'\fR format, which no formattershould claim to understand (by policy).  Mark the end of the blockwith <=end>..PP.Vb 1\&        # program is here\&        \&        =begin comment\&        \&        all of this stuff\&        \&        here will be ignored\&        by everyone\&        \&        =end comment\&        \&        =cut\&        \&        # program continues.Ve.PPThe pod directives cannot go just anywhere.  You must put apod directive where the parser is expecting a new statement,not just in the middle of an expression or some otherarbitrary grammar production..PPSee perlpod for more details..Sh "How do I clear a package?".IX Subsection "How do I clear a package?"Use this code, provided by Mark-Jason Dominus:.PP.Vb 10\&        sub scrub_package {\&                no strict \*(Aqrefs\*(Aq;\&                my $pack = shift;\&                die "Shouldn\*(Aqt delete main package"\&                        if $pack eq "" || $pack eq "main";\&                my $stash = *{$pack . \*(Aq::\*(Aq}{HASH};\&                my $name;\&                foreach $name (keys %$stash) {\&                        my $fullname = $pack . \*(Aq::\*(Aq . $name;\&                        # Get rid of everything with that name.\&                        undef $$fullname;\&                        undef @$fullname;\&                        undef %$fullname;\&                        undef &$fullname;\&                        undef *$fullname;\&        }\&        }.Ve.PPOr, if you're using a recent release of Perl, you canjust use the \fISymbol::delete_package()\fR function instead..Sh "How can I use a variable as a variable name?".IX Subsection "How can I use a variable as a variable name?"Beginners often think they want to have a variable contain the nameof a variable..PP.Vb 3\&        $fred    = 23;\&        $varname = "fred";\&        ++$$varname;         # $fred now 24.Ve.PPThis works \fIsometimes\fR, but it is a very bad idea for two reasons..PPThe first reason is that this technique \fIonly works on globalvariables\fR.  That means that if \f(CW$fred\fR is a lexical variable createdwith \fImy()\fR in the above example, the code wouldn't work at all: you'daccidentally access the global and skip right over the private lexicalaltogether.  Global variables are bad because they can easily collideaccidentally and in general make for non-scalable and confusing code..PPSymbolic references are forbidden under the \f(CW\*(C`use strict\*(C'\fR pragma.They are not true references and consequently are not reference countedor garbage collected..PPThe other reason why using a variable to hold the name of anothervariable is a bad idea is that the question often stems from a lack ofunderstanding of Perl data structures, particularly hashes.  By usingsymbolic references, you are just using the package's symbol-table hash(like \f(CW%main::\fR) instead of a user-defined hash.  The solution is touse your own hash or a real reference instead..PP.Vb 3\&        $USER_VARS{"fred"} = 23;\&        $varname = "fred";\&        $USER_VARS{$varname}++;  # not $$varname++.Ve.PPThere we're using the \f(CW%USER_VARS\fR hash instead of symbolic references.Sometimes this comes up in reading strings from the user with variablereferences and wanting to expand them to the values of your perlprogram's variables.  This is also a bad idea because it conflates theprogram-addressable namespace and the user-addressable one.  Instead ofreading a string and expanding it to the actual contents of your program'sown variables:.PP.Vb 2\&        $str = \*(Aqthis has a $fred and $barney in it\*(Aq;\&        $str =~ s/(\e$\ew+)/$1/eeg;                 # need double eval.Ve.PPit would be better to keep a hash around like \f(CW%USER_VARS\fR and havevariable references actually refer to entries in that hash:.PP.Vb 1\&        $str =~ s/\e$(\ew+)/$USER_VARS{$1}/g;   # no /e here at all.Ve.PPThat's faster, cleaner, and safer than the previous approach.  Of course,you don't need to use a dollar sign.  You could use your own scheme tomake it less confusing, like bracketed percent symbols, etc..PP.Vb 2\&        $str = \*(Aqthis has a %fred% and %barney% in it\*(Aq;\&        $str =~ s/%(\ew+)%/$USER_VARS{$1}/g;   # no /e here at all.Ve.PPAnother reason that folks sometimes think they want a variable tocontain the name of a variable is because they don't know how to buildproper data structures using hashes.  For example, let's say theywanted two hashes in their program: \f(CW%fred\fR and \f(CW%barney\fR, and that theywanted to use another scalar variable to refer to those by name..PP.Vb 2\&        $name = "fred";\&        $$name{WIFE} = "wilma";     # set %fred\&\&        $name = "barney";\&        $$name{WIFE} = "betty"; # set %barney.Ve.PPThis is still a symbolic reference, and is still saddled with theproblems enumerated above.  It would be far better to write:.PP.Vb 2\&        $folks{"fred"}{WIFE}   = "wilma";\&        $folks{"barney"}{WIFE} = "betty";.Ve.PPAnd just use a multilevel hash to start with..PPThe only times that you absolutely \fImust\fR use symbolic references arewhen you really must refer to the symbol table.  This may be because it'ssomething that can't take a real reference to, such as a format name.Doing so may also be important for method calls, since these always gothrough the symbol table for resolution..PPIn those cases, you would turn off \f(CW\*(C`strict \*(Aqrefs\*(Aq\*(C'\fR temporarily so youcan play around with the symbol table.  For example:.PP.Vb 5\&        @colors = qw(red blue green yellow orange purple violet);\&        for my $name (@colors) {\&                no strict \*(Aqrefs\*(Aq;  # renege for the block\&                *$name = sub { "<FONT COLOR=\*(Aq$name\*(Aq>@_</FONT>" };\&        }.Ve.PPAll those functions (\fIred()\fR, \fIblue()\fR, \fIgreen()\fR, etc.) appear to be separate,but the real code in the closure actually was compiled only once..PPSo, sometimes you might want to use symbolic references to directlymanipulate the symbol table.  This doesn't matter for formats, handles, andsubroutines, because they are always global\*(--you can't use \fImy()\fR on them.For scalars, arrays, and hashes, though\*(--and usually for subroutines\*(--you probably only want to use hard references..ie n .Sh "What does ""bad interpreter"" mean?".el .Sh "What does ``bad interpreter'' mean?".IX Subsection "What does bad interpreter mean?"(contributed by brian d foy).PPThe \*(L"bad interpreter\*(R" message comes from the shell, not perl.  Theactual message may vary depending on your platform, shell, and localesettings..PPIf you see \*(L"bad interpreter \- no such file or directory\*(R", the firstline in your perl script (the \*(L"shebang\*(R" line) does not contain theright path to perl (or any other program capable of running scripts).Sometimes this happens when you move the script from one machine toanother and each machine has a different path to perl\-\-/usr/bin/perlversus /usr/local/bin/perl for instance. It may also indicatethat the source machine has \s-1CRLF\s0 line terminators and thedestination machine has \s-1LF\s0 only: the shell tries to find/usr/bin/perl<\s-1CR\s0>, but can't..PPIf you see \*(L"bad interpreter: Permission denied\*(R", you need to make yourscript executable..PPIn either case, you should still be able to run the scripts with perlexplicitly:.PP.Vb 1\&        % perl script.pl.Ve.PPIf you get a message like \*(L"perl: command not found\*(R", perl is not inyour \s-1PATH\s0, which might also mean that the location of perl is notwhere you expect it so you need to adjust your shebang line..SH "REVISION".IX Header "REVISION"Revision: \f(CW$Revision:\fR 10100 $.PPDate: \f(CW$Date:\fR 2007\-10\-21 20:59:30 +0200 (Sun, 21 Oct 2007) $.PPSee perlfaq for source control details and availability..SH "AUTHOR AND COPYRIGHT".IX Header "AUTHOR AND COPYRIGHT"Copyright (c) 1997\-2007 Tom Christiansen, Nathan Torkington, andother authors as noted. All rights reserved..PPThis documentation is free; you can redistribute it and/or modify itunder the same terms as Perl itself..PPIrrespective of its distribution, all code examples in this fileare hereby placed into the public domain.  You are permitted andencouraged to use this code in your own programs for funor for profit as you see fit.  A simple comment in the code givingcredit would be courteous but is not required.

⌨️ 快捷键说明

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