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

📄 perlfaq8.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 4 页
字号:
.PP.Vb 3\&        if (\-t STDIN && \-t STDOUT) {\&                print "Now what? ";\&                }.Ve.PPOn \s-1POSIX\s0 systems, you can test whether your own process group matchesthe current process group of your controlling terminal as follows:.PP.Vb 1\&        use POSIX qw/getpgrp tcgetpgrp/;\&\&        # Some POSIX systems, such as Linux, can be\&        # without a /dev/tty at boot time.\&        if (!open(TTY, "/dev/tty")) {\&                print "no tty\en";\&        } else {\&                $tpgrp = tcgetpgrp(fileno(*TTY));\&                $pgrp = getpgrp();\&                if ($tpgrp == $pgrp) {\&                        print "foreground\en";\&                } else {\&                        print "background\en";\&                }\&        }.Ve.Sh "How do I timeout a slow event?".IX Subsection "How do I timeout a slow event?"Use the \fIalarm()\fR function, probably in conjunction with a signalhandler, as documented in \*(L"Signals\*(R" in perlipc and the section on\&\*(L"Signals\*(R" in the Camel.  You may instead use the more flexibleSys::AlarmCall module available from \s-1CPAN\s0..PPThe \fIalarm()\fR function is not implemented on all versions of Windows.Check the documentation for your specific version of Perl..Sh "How do I set \s-1CPU\s0 limits?".IX Xref "BSD::Resource limit CPU".IX Subsection "How do I set CPU limits?"(contributed by Xho).PPUse the \f(CW\*(C`BSD::Resource\*(C'\fR module from \s-1CPAN\s0. As an example:.PP.Vb 2\&        use BSD::Resource;\&        setrlimit(RLIMIT_CPU,10,20) or die $!;.Ve.PPThis sets the soft and hard limits to 10 and 20 seconds, respectively.After 10 seconds of time spent running on the \s-1CPU\s0 (not \*(L"wall\*(R" time),the process will be sent a signal (\s-1XCPU\s0 on some systems) which, if nottrapped, will cause the process to terminate.  If that signal istrapped, then after 10 more seconds (20 seconds in total) the processwill be killed with a non-trappable signal..PPSee the \f(CW\*(C`BSD::Resource\*(C'\fR and your systems documentation for the gorydetails..Sh "How do I avoid zombies on a Unix system?".IX Subsection "How do I avoid zombies on a Unix system?"Use the reaper code from \*(L"Signals\*(R" in perlipc to call \fIwait()\fR when a\&\s-1SIGCHLD\s0 is received, or else use the double-fork technique describedin \*(L"How do I start a process in the background?\*(R" in perlfaq8..Sh "How do I use an \s-1SQL\s0 database?".IX Subsection "How do I use an SQL database?"The \s-1DBI\s0 module provides an abstract interface to most databaseservers and types, including Oracle, \s-1DB2\s0, Sybase, mysql, Postgresql,\&\s-1ODBC\s0, and flat files.  The \s-1DBI\s0 module accesses each database typethrough a database driver, or \s-1DBD\s0.  You can see a complete list ofavailable drivers on \s-1CPAN:\s0 http://www.cpan.org/modules/by\-module/DBD/ .You can read more about \s-1DBI\s0 on http://dbi.perl.org ..PPOther modules provide more specific access: Win32::ODBC, Alzabo, iodbc,and others found on \s-1CPAN\s0 Search: http://search.cpan.org ..Sh "How do I make a \fIsystem()\fP exit on control-C?".IX Subsection "How do I make a system() exit on control-C?"You can't.  You need to imitate the \fIsystem()\fR call (see perlipc forsample code) and then have a signal handler for the \s-1INT\s0 signal thatpasses the signal on to the subprocess.  Or you can check for it:.PP.Vb 2\&        $rc = system($cmd);\&        if ($rc & 127) { die "signal death" }.Ve.Sh "How do I open a file without blocking?".IX Subsection "How do I open a file without blocking?"If you're lucky enough to be using a system that supportsnon-blocking reads (most Unixish systems do), you need only to use theO_NDELAY or O_NONBLOCK flag from the Fcntl module in conjunction with\&\fIsysopen()\fR:.PP.Vb 3\&        use Fcntl;\&        sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT, 0644)\&                or die "can\*(Aqt open /foo/somefile: $!":.Ve.Sh "How do I tell the difference between errors from the shell and perl?".IX Subsection "How do I tell the difference between errors from the shell and perl?"(answer contributed by brian d foy).PPWhen you run a Perl script, something else is running the script for you,and that something else may output error messages.  The script mightemit its own warnings and error messages.  Most of the time you cannottell who said what..PPYou probably cannot fix the thing that runs perl, but you can change howperl outputs its warnings by defining a custom warning and die functions..PPConsider this script, which has an error you may not notice immediately..PP.Vb 1\&        #!/usr/locl/bin/perl\&\&        print "Hello World\en";.Ve.PPI get an error when I run this from my shell (which happens to bebash).  That may look like perl forgot it has a \fIprint()\fR function,but my shebang line is not the path to perl, so the shell runs thescript, and I get the error..PP.Vb 2\&        $ ./test\&        ./test: line 3: print: command not found.Ve.PPA quick and dirty fix involves a little bit of code, but this may be allyou need to figure out the problem..PP.Vb 1\&        #!/usr/bin/perl \-w\&\&        BEGIN {\&        $SIG{_\|_WARN_\|_} = sub{ print STDERR "Perl: ", @_; };\&        $SIG{_\|_DIE_\|_}  = sub{ print STDERR "Perl: ", @_; exit 1};\&        }\&\&        $a = 1 + undef;\&        $x / 0;\&        _\|_END_\|_.Ve.PPThe perl message comes out with \*(L"Perl\*(R" in front.  The \s-1BEGIN\s0 blockworks at compile time so all of the compilation errors and warningsget the \*(L"Perl:\*(R" prefix too..PP.Vb 7\&        Perl: Useless use of division (/) in void context at ./test line 9.\&        Perl: Name "main::a" used only once: possible typo at ./test line 8.\&        Perl: Name "main::x" used only once: possible typo at ./test line 9.\&        Perl: Use of uninitialized value in addition (+) at ./test line 8.\&        Perl: Use of uninitialized value in division (/) at ./test line 9.\&        Perl: Illegal division by zero at ./test line 9.\&        Perl: Illegal division by zero at \-e line 3..Ve.PPIf I don't see that \*(L"Perl:\*(R", it's not from perl..PPYou could also just know all the perl errors, and although there aresome people who may know all of them, you probably don't.  However, theyall should be in the perldiag manpage. If you don't find the error inthere, it probably isn't a perl error..PPLooking up every message is not the easiest way, so let perl to do itfor you.  Use the diagnostics pragma with turns perl's normal messagesinto longer discussions on the topic..PP.Vb 1\&        use diagnostics;.Ve.PPIf you don't get a paragraph or two of expanded discussion, itmight not be perl's message..Sh "How do I install a module from \s-1CPAN\s0?".IX Subsection "How do I install a module from CPAN?"The easiest way is to have a module also named \s-1CPAN\s0 do it for you.This module comes with perl version 5.004 and later..PP.Vb 1\&        $ perl \-MCPAN \-e shell\&\&        cpan shell \-\- CPAN exploration and modules installation (v1.59_54)\&        ReadLine support enabled\&\&        cpan> install Some::Module.Ve.PPTo manually install the \s-1CPAN\s0 module, or any well-behaved \s-1CPAN\s0 modulefor that matter, follow these steps:.IP "1." 4Unpack the source into a temporary area..IP "2." 4.Vb 1\&        perl Makefile.PL.Ve.IP "3." 4.Vb 1\&        make.Ve.IP "4." 4.Vb 1\&        make test.Ve.IP "5." 4.Vb 1\&        make install.Ve.PPIf your version of perl is compiled without dynamic loading, then youjust need to replace step 3 (\fBmake\fR) with \fBmake perl\fR and you willget a new \fIperl\fR binary with your extension linked in..PPSee ExtUtils::MakeMaker for more details on building extensions.See also the next question, \*(L"What's the difference between requireand use?\*(R"..Sh "What's the difference between require and use?".IX Subsection "What's the difference between require and use?"Perl offers several different ways to include code from one file intoanother.  Here are the deltas between the various inclusion constructs:.PP.Vb 3\&        1)  do $file is like eval \`cat $file\`, except the former\&        1.1: searches @INC and updates %INC.\&        1.2: bequeaths an *unrelated* lexical scope on the eval\*(Aqed code.\&\&        2)  require $file is like do $file, except the former\&        2.1: checks for redundant loading, skipping already loaded files.\&        2.2: raises an exception on failure to find, compile, or execute $file.\&\&        3)  require Module is like require "Module.pm", except the former\&        3.1: translates each "::" into your system\*(Aqs directory separator.\&        3.2: primes the parser to disambiguate class Module as an indirect object.\&\&        4)  use Module is like require Module, except the former\&        4.1: loads the module at compile time, not run\-time.\&        4.2: imports symbols and semantics from that package to the current one..Ve.PPIn general, you usually want \f(CW\*(C`use\*(C'\fR and a proper Perl module..Sh "How do I keep my own module/library directory?".IX Subsection "How do I keep my own module/library directory?"When you build modules, tell Perl where to install the modules..PPFor \f(CW\*(C`Makefile.PL\*(C'\fR\-based distributions, use the \s-1PREFIX\s0 and \s-1LIB\s0 optionswhen generating Makefiles:.PP.Vb 1\&        perl Makefile.PL PREFIX=/mydir/perl LIB=/mydir/perl/lib.Ve.PPYou can set this in your \s-1CPAN\s0.pm configuration so modules automatically installin your private library directory when you use the \s-1CPAN\s0.pm shell:.PP.Vb 3\&        % cpan\&        cpan> o conf makepl_arg PREFIX=/mydir/perl,LIB=/mydir/perl/lib\&        cpan> o conf commit.Ve.PPFor \f(CW\*(C`Build.PL\*(C'\fR\-based distributions, use the \-\-install_base option:.PP.Vb 1\&        perl Build.PL \-\-install_base /mydir/perl.Ve.PPYou can configure \s-1CPAN\s0.pm to automatically use this option too:.PP.Vb 3\&        % cpan\&        cpan> o conf mbuild_arg \-\-install_base /mydir/perl\&        cpan> o conf commit.Ve.Sh "How do I add the directory my program lives in to the module/library search path?".IX Subsection "How do I add the directory my program lives in to the module/library search path?"(contributed by brian d foy).PPIf you know the directory already, you can add it to \f(CW@INC\fR as you wouldfor any other directory. You might <use lib> if you know the directoryat compile time:.PP.Vb 1\&        use lib $directory;.Ve.PPThe trick in this task is to find the directory. Before your script doesanything else (such as a \f(CW\*(C`chdir\*(C'\fR), you can get the current workingdirectory with the \f(CW\*(C`Cwd\*(C'\fR module, which comes with Perl:.PP.Vb 4\&        BEGIN {\&                use Cwd;\&                our $directory = cwd;\&                }\&        \&        use lib $directory;.Ve.PPYou can do a similar thing with the value of \f(CW$0\fR, which holds thescript name. That might hold a relative path, but \f(CW\*(C`rel2abs\*(C'\fR can turnit into an absolute path. Once you have the.PP.Vb 3\&        BEGIN { \&                use File::Spec::Functions qw(rel2abs);\&                use File::Basename qw(dirname);\&                \&                my $path   = rel2abs( $0 );\&                our $directory = dirname( $path );\&                }\&                \&        use lib $directory;.Ve.PPThe \f(CW\*(C`FindBin\*(C'\fR module, which comes with Perl, might work. It searchesthrough \f(CW$ENV{PATH}\fR (so your script has to be in one of thosedirectories). You can then use that directory (in \f(CW$FindBin::Bin\fR)to locate nearby directories you want to add:.PP.Vb 2\&        use FindBin;\&        use lib "$FindBin::Bin/../lib";.Ve.Sh "How do I add a directory to my include path (@INC) at runtime?".IX Subsection "How do I add a directory to my include path (@INC) at runtime?"Here are the suggested ways of modifying your include path, includingenvironment variables, run-time switches, and in-code statements:.IP "the \s-1PERLLIB\s0 environment variable" 4.IX Item "the PERLLIB environment variable".Vb 2\&        $ export PERLLIB=/path/to/my/dir\&        $ perl program.pl.Ve.IP "the \s-1PERL5LIB\s0 environment variable" 4.IX Item "the PERL5LIB environment variable".Vb 2\&        $ export PERL5LIB=/path/to/my/dir\&        $ perl program.pl.Ve.IP "the perl \-Idir command line flag" 4.IX Item "the perl -Idir command line flag".Vb 1\&        $ perl \-I/path/to/my/dir program.pl.Ve.IP "the use lib pragma:" 4.IX Item "the use lib pragma:".Vb 1\&        use lib "$ENV{HOME}/myown_perllib";.Ve.PPThe last is particularly useful because it knows about machinedependent architectures.  The lib.pm pragmatic module was firstincluded with the 5.002 release of Perl..Sh "What is socket.ph and where do I get it?".IX Subsection "What is socket.ph and where do I get it?"It's a Perl 4 style file defining values for system networkingconstants.  Sometimes it is built using h2ph when Perl is installed,but other times it is not.  Modern programs \f(CW\*(C`use Socket;\*(C'\fR instead..SH "REVISION".IX Header "REVISION"Revision: \f(CW$Revision:\fR 10183 $.PPDate: \f(CW$Date:\fR 2007\-11\-07 09:35:12 +0100 (Wed, 07 Nov 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 + -