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

📄 perlfaq5.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 5 页
字号:
\&\&        my ($term, $oterm, $echo, $noecho, $fd_stdin);\&\&        $fd_stdin = fileno(STDIN);\&\&        $term     = POSIX::Termios\->new();\&        $term\->getattr($fd_stdin);\&        $oterm     = $term\->getlflag();\&\&        $echo     = ECHO | ECHOK | ICANON;\&        $noecho   = $oterm & ~$echo;\&\&        sub cbreak {\&                $term\->setlflag($noecho);\&                $term\->setcc(VTIME, 1);\&                $term\->setattr($fd_stdin, TCSANOW);\&                }\&\&        sub cooked {\&                $term\->setlflag($oterm);\&                $term\->setcc(VTIME, 0);\&                $term\->setattr($fd_stdin, TCSANOW);\&                }\&\&        sub getone {\&                my $key = \*(Aq\*(Aq;\&                cbreak();\&                sysread(STDIN, $key, 1);\&                cooked();\&                return $key;\&                }\&\&        }\&\&        END { cooked() }.Ve.PPThe Term::ReadKey module from \s-1CPAN\s0 may be easier to use.  Recent versionsinclude also support for non-portable systems as well..PP.Vb 8\&        use Term::ReadKey;\&        open(TTY, "</dev/tty");\&        print "Gimme a char: ";\&        ReadMode "raw";\&        $key = ReadKey 0, *TTY;\&        ReadMode "normal";\&        printf "\enYou said %s, char number %03d\en",\&                $key, ord $key;.Ve.Sh "How can I tell whether there's a character waiting on a filehandle?".IX Subsection "How can I tell whether there's a character waiting on a filehandle?"The very first thing you should do is look into getting the Term::ReadKeyextension from \s-1CPAN\s0.  As we mentioned earlier, it now even has limitedsupport for non-portable (read: not open systems, closed, proprietary,not \s-1POSIX\s0, not Unix, etc) systems..PPYou should also check out the Frequently Asked Questions list incomp.unix.* for things like this: the answer is essentially the same.It's very system dependent.  Here's one solution that works on \s-1BSD\s0systems:.PP.Vb 5\&        sub key_ready {\&                my($rin, $nfd);\&                vec($rin, fileno(STDIN), 1) = 1;\&                return $nfd = select($rin,undef,undef,0);\&                }.Ve.PPIf you want to find out how many characters are waiting, there'salso the \s-1FIONREAD\s0 ioctl call to be looked at.  The \fIh2ph\fR tool thatcomes with Perl tries to convert C include files to Perl code, whichcan be \f(CW\*(C`require\*(C'\fRd.  \s-1FIONREAD\s0 ends up defined as a function in the\&\fIsys/ioctl.ph\fR file:.PP.Vb 1\&        require \*(Aqsys/ioctl.ph\*(Aq;\&\&        $size = pack("L", 0);\&        ioctl(FH, FIONREAD(), $size)    or die "Couldn\*(Aqt call ioctl: $!\en";\&        $size = unpack("L", $size);.Ve.PPIf \fIh2ph\fR wasn't installed or doesn't work for you, you can\&\fIgrep\fR the include files by hand:.PP.Vb 2\&        % grep FIONREAD /usr/include/*/*\&        /usr/include/asm/ioctls.h:#define FIONREAD      0x541B.Ve.PPOr write a small C program using the editor of champions:.PP.Vb 9\&        % cat > fionread.c\&        #include <sys/ioctl.h>\&        main() {\&            printf("%#08x\en", FIONREAD);\&        }\&        ^D\&        % cc \-o fionread fionread.c\&        % ./fionread\&        0x4004667f.Ve.PPAnd then hard code it, leaving porting as an exercise to your successor..PP.Vb 1\&        $FIONREAD = 0x4004667f;         # XXX: opsys dependent\&\&        $size = pack("L", 0);\&        ioctl(FH, $FIONREAD, $size)     or die "Couldn\*(Aqt call ioctl: $!\en";\&        $size = unpack("L", $size);.Ve.PP\&\s-1FIONREAD\s0 requires a filehandle connected to a stream, meaning that sockets,pipes, and tty devices work, but \fInot\fR files..ie n .Sh "How do I do a ""tail \-f"" in perl?".el .Sh "How do I do a \f(CWtail \-f\fP in perl?".IX Xref "tail IO::Handle File::Tail clearerr".IX Subsection "How do I do a tail -f in perl?"First try.PP.Vb 1\&        seek(GWFILE, 0, 1);.Ve.PPThe statement \f(CW\*(C`seek(GWFILE, 0, 1)\*(C'\fR doesn't change the current position,but it does clear the end-of-file condition on the handle, so that thenext \f(CW\*(C`<GWFILE>\*(C'\fR makes Perl try again to read something..PPIf that doesn't work (it relies on features of your stdio implementation),then you need something more like this:.PP.Vb 7\&        for (;;) {\&          for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {\&            # search for some stuff and put it into files\&          }\&          # sleep for a while\&          seek(GWFILE, $curpos, 0);  # seek to where we had been\&        }.Ve.PPIf this still doesn't work, look into the \f(CW\*(C`clearerr\*(C'\fR methodfrom \f(CW\*(C`IO::Handle\*(C'\fR, which resets the error and end-of-file stateson the handle..PPThere's also a \f(CW\*(C`File::Tail\*(C'\fR module from \s-1CPAN\s0..Sh "How do I \fIdup()\fP a filehandle in Perl?".IX Xref "dup".IX Subsection "How do I dup() a filehandle in Perl?"If you check \*(L"open\*(R" in perlfunc, you'll see that several of the waysto call \fIopen()\fR should do the trick.  For example:.PP.Vb 2\&        open(LOG, ">>/foo/logfile");\&        open(STDERR, ">&LOG");.Ve.PPOr even with a literal numeric descriptor:.PP.Vb 2\&   $fd = $ENV{MHCONTEXTFD};\&   open(MHCONTEXT, "<&=$fd");   # like fdopen(3S).Ve.PPNote that \*(L"<&STDIN\*(R" makes a copy, but \*(L"<&=STDIN\*(R" makean alias.  That means if you close an aliased handle, allaliases become inaccessible.  This is not true witha copied one..PPError checking, as always, has been left as an exercise for the reader..Sh "How do I close a file descriptor by number?".IX Xref "file, closing file descriptors POSIX close".IX Subsection "How do I close a file descriptor by number?"If, for some reason, you have a file descriptor instead of afilehandle (perhaps you used \f(CW\*(C`POSIX::open\*(C'\fR), you can use the\&\f(CW\*(C`close()\*(C'\fR function from the \f(CW\*(C`POSIX\*(C'\fR module:.PP.Vb 1\&        use POSIX ();\&        \&        POSIX::close( $fd );.Ve.PPThis should rarely be necessary, as the Perl \f(CW\*(C`close()\*(C'\fR function is to beused for things that Perl opened itself, even if it was a dup of anumeric descriptor as with \f(CW\*(C`MHCONTEXT\*(C'\fR above.  But if you really haveto, you may be able to do this:.PP.Vb 3\&        require \*(Aqsys/syscall.ph\*(Aq;\&        $rc = syscall(&SYS_close, $fd + 0);  # must force numeric\&        die "can\*(Aqt sysclose $fd: $!" unless $rc == \-1;.Ve.PPOr, just use the fdopen(3S) feature of \f(CW\*(C`open()\*(C'\fR:.PP.Vb 4\&        {\&        open my( $fh ), "<&=$fd" or die "Cannot reopen fd=$fd: $!";\&        close $fh;\&        }.Ve.ie n .Sh "Why can't I use ""C:\etemp\efoo"" in \s-1DOS\s0 paths?  Why doesn't `C:\etemp\efoo.exe` work?".el .Sh "Why can't I use ``C:\etemp\efoo'' in \s-1DOS\s0 paths?  Why doesn't `C:\etemp\efoo.exe` work?".IX Xref "filename, DOS issues".IX Subsection "Why can't I use C:tempfoo in DOS paths?  Why doesn't `C:tempfoo.exe` work?"Whoops!  You just put a tab and a formfeed into that filename!Remember that within double quoted strings (\*(L"like\ethis\*(R"), thebackslash is an escape character.  The full list of these is in\&\*(L"Quote and Quote-like Operators\*(R" in perlop.  Unsurprisingly, you don'thave a file called \*(L"c:(tab)emp(formfeed)oo\*(R" or\&\*(L"c:(tab)emp(formfeed)oo.exe\*(R" on your legacy \s-1DOS\s0 filesystem..PPEither single-quote your strings, or (preferably) use forward slashes.Since all \s-1DOS\s0 and Windows versions since something like MS-DOS 2.0 or sohave treated \f(CW\*(C`/\*(C'\fR and \f(CW\*(C`\e\*(C'\fR the same in a path, you might as well use theone that doesn't clash with Perl\*(--or the \s-1POSIX\s0 shell, \s-1ANSI\s0 C and \*(C+,awk, Tcl, Java, or Python, just to mention a few.  \s-1POSIX\s0 pathsare more portable, too..ie n .Sh "Why doesn't glob(""*.*"") get all the files?".el .Sh "Why doesn't glob(``*.*'') get all the files?".IX Xref "glob".IX Subsection "Why doesn't glob(*.*) get all the files?"Because even on non-Unix ports, Perl's glob function follows standardUnix globbing semantics.  You'll need \f(CW\*(C`glob("*")\*(C'\fR to get all (non-hidden)files.  This makes \fIglob()\fR portable even to legacy systems.  Yourport may include proprietary globbing functions as well.  Check itsdocumentation for details..ie n .Sh "Why does Perl let me delete read-only files?  Why does ""\-i"" clobber protected files?  Isn't this a bug in Perl?".el .Sh "Why does Perl let me delete read-only files?  Why does \f(CW\-i\fP clobber protected files?  Isn't this a bug in Perl?".IX Subsection "Why does Perl let me delete read-only files?  Why does -i clobber protected files?  Isn't this a bug in Perl?"This is elaborately and painstakingly described in the\&\fIfile-dir-perms\fR article in the \*(L"Far More Than You Ever Wanted ToKnow\*(R" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz ..PPThe executive summary: learn how your filesystem works.  Thepermissions on a file say what can happen to the data in that file.The permissions on a directory say what can happen to the list offiles in that directory.  If you delete a file, you're removing itsname from the directory (so the operation depends on the permissionsof the directory, not of the file).  If you try to write to the file,the permissions of the file govern whether you're allowed to..Sh "How do I select a random line from a file?".IX Xref "file, selecting a random line".IX Subsection "How do I select a random line from a file?"Here's an algorithm from the Camel Book:.PP.Vb 2\&        srand;\&        rand($.) < 1 && ($line = $_) while <>;.Ve.PPThis has a significant advantage in space over reading the whole filein.  You can find a proof of this method in \fIThe Art of ComputerProgramming\fR, Volume 2, Section 3.4.2, by Donald E. Knuth..PPYou can use the File::Random module which provides a functionfor that algorithm:.PP.Vb 2\&        use File::Random qw/random_line/;\&        my $line = random_line($filename);.Ve.PPAnother way is to use the Tie::File module, which treats the entirefile as an array.  Simply access a random array element..Sh "Why do I get weird spaces when I print an array of lines?".IX Subsection "Why do I get weird spaces when I print an array of lines?"Saying.PP.Vb 1\&        print "@lines\en";.Ve.PPjoins together the elements of \f(CW@lines\fR with a space between them.If \f(CW@lines\fR were \f(CW\*(C`("little", "fluffy", "clouds")\*(C'\fR then the abovestatement would print.PP.Vb 1\&        little fluffy clouds.Ve.PPbut if each element of \f(CW@lines\fR was a line of text, ending a newlinecharacter \f(CW\*(C`("little\en", "fluffy\en", "clouds\en")\*(C'\fR then it would print:.PP.Vb 3\&        little\&         fluffy\&         clouds.Ve.PPIf your array contains lines, just print them:.PP.Vb 1\&        print @lines;.Ve.SH "REVISION".IX Header "REVISION"Revision: \f(CW$Revision:\fR 10126 $.PPDate: \f(CW$Date:\fR 2007\-10\-27 21:29:20 +0200 (Sat, 27 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 here are in the publicdomain.  You are permitted and encouraged to use this code and anyderivatives thereof in your own programs for fun or for profit as yousee fit.  A simple comment in the code giving credit to the \s-1FAQ\s0 wouldbe courteous but is not required.

⌨️ 快捷键说明

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