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

📄 perlopentut.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 4 页
字号:
.Vb 3\&    @ARGV = qw(.) unless @ARGV;\&    use File::Find;\&    find sub { print $File::Find::name, \-d && \*(Aq/\*(Aq, "\en" }, @ARGV;.Ve.PPThis finds all bogus symbolic links beneath a particular directory:.PP.Vb 1\&    find sub { print "$File::Find::name\en" if \-l && !\-e }, $dir;.Ve.PPAs you see, with symbolic links, you can just pretend that it iswhat it points to.  Or, if you want to know \fIwhat\fR it points to, then\&\f(CW\*(C`readlink\*(C'\fR is called for:.PP.Vb 7\&    if (\-l $file) {\&        if (defined($whither = readlink($file))) {\&            print "$file points to $whither\en";\&        } else {\&            print "$file points nowhere: $!\en";\&        } \&    }.Ve.Sh "Opening Named Pipes".IX Subsection "Opening Named Pipes"Named pipes are a different matter.  You pretend they're regular files,but their opens will normally block until there is both a reader anda writer.  You can read more about them in \*(L"Named Pipes\*(R" in perlipc.Unix-domain sockets are rather different beasts as well; they'redescribed in \*(L"Unix-Domain \s-1TCP\s0 Clients and Servers\*(R" in perlipc..PPWhen it comes to opening devices, it can be easy and it can be tricky.We'll assume that if you're opening up a block device, you know whatyou're doing.  The character devices are more interesting.  These aretypically used for modems, mice, and some kinds of printers.  This isdescribed in \*(L"How do I read and write the serial port?\*(R" in perlfaq8It's often enough to open them carefully:.PP.Vb 5\&    sysopen(TTYIN, "/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY)\&                # (O_NOCTTY no longer needed on POSIX systems)\&        or die "can\*(Aqt open /dev/ttyS1: $!";\&    open(TTYOUT, "+>&TTYIN")\&        or die "can\*(Aqt dup TTYIN: $!";\&\&    $ofh = select(TTYOUT); $| = 1; select($ofh);\&\&    print TTYOUT "+++at\e015";\&    $answer = <TTYIN>;.Ve.PPWith descriptors that you haven't opened using \f(CW\*(C`sysopen\*(C'\fR, such assockets, you can set them to be non-blocking using \f(CW\*(C`fcntl\*(C'\fR:.PP.Vb 5\&    use Fcntl;\&    my $old_flags = fcntl($handle, F_GETFL, 0) \&        or die "can\*(Aqt get flags: $!";\&    fcntl($handle, F_SETFL, $old_flags | O_NONBLOCK) \&        or die "can\*(Aqt set non blocking: $!";.Ve.PPRather than losing yourself in a morass of twisting, turning \f(CW\*(C`ioctl\*(C'\fRs,all dissimilar, if you're going to manipulate ttys, it's best tomake calls out to the \fIstty\fR\|(1) program if you have it, or else use theportable \s-1POSIX\s0 interface.  To figure this all out, you'll need to read the\&\fItermios\fR\|(3) manpage, which describes the \s-1POSIX\s0 interface to tty devices,and then \s-1POSIX\s0, which describes Perl's interface to \s-1POSIX\s0.  There arealso some high-level modules on \s-1CPAN\s0 that can help you with these games.Check out Term::ReadKey and Term::ReadLine..Sh "Opening Sockets".IX Subsection "Opening Sockets"What else can you open?  To open a connection using sockets, you won't useone of Perl's two open functions.  See \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc for that.  Here's an example.  Once you have it, you can use \s-1FH\s0 as a bidirectional filehandle..PP.Vb 2\&    use IO::Socket;\&    local *FH = IO::Socket::INET\->new("www.perl.com:80");.Ve.PPFor opening up a \s-1URL\s0, the \s-1LWP\s0 modules from \s-1CPAN\s0 are just whatthe doctor ordered.  There's no filehandle interface, butit's still easy to get the contents of a document:.PP.Vb 2\&    use LWP::Simple;\&    $doc = get(\*(Aqhttp://www.linpro.no/lwp/\*(Aq);.Ve.Sh "Binary Files".IX Subsection "Binary Files"On certain legacy systems with what could charitably be called terminallyconvoluted (some would say broken) I/O models, a file isn't a file\*(--atleast, not with respect to the C standard I/O library.  On these oldsystems whose libraries (but not kernels) distinguish between text andbinary streams, to get files to behave properly you'll have to bend overbackwards to avoid nasty problems.  On such infelicitous systems, socketsand pipes are already opened in binary mode, and there is currently noway to turn that off.  With files, you have more options..PPAnother option is to use the \f(CW\*(C`binmode\*(C'\fR function on the appropriatehandles before doing regular I/O on them:.PP.Vb 3\&    binmode(STDIN);\&    binmode(STDOUT);\&    while (<STDIN>) { print }.Ve.PPPassing \f(CW\*(C`sysopen\*(C'\fR a non-standard flag option will also open the file inbinary mode on those systems that support it.  This is the equivalent ofopening the file normally, then calling \f(CW\*(C`binmode\*(C'\fR on the handle..PP.Vb 2\&    sysopen(BINDAT, "records.data", O_RDWR | O_BINARY)\&        || die "can\*(Aqt open records.data: $!";.Ve.PPNow you can use \f(CW\*(C`read\*(C'\fR and \f(CW\*(C`print\*(C'\fR on that handle without worryingabout the non-standard system I/O library breaking your data.  It's nota pretty picture, but then, legacy systems seldom are.  \s-1CP/M\s0 will bewith us until the end of days, and after..PPOn systems with exotic I/O systems, it turns out that, astonishinglyenough, even unbuffered I/O using \f(CW\*(C`sysread\*(C'\fR and \f(CW\*(C`syswrite\*(C'\fR might dosneaky data mutilation behind your back..PP.Vb 3\&    while (sysread(WHENCE, $buf, 1024)) {\&        syswrite(WHITHER, $buf, length($buf));\&    }.Ve.PPDepending on the vicissitudes of your runtime system, even these callsmay need \f(CW\*(C`binmode\*(C'\fR or \f(CW\*(C`O_BINARY\*(C'\fR first.  Systems known to be free ofsuch difficulties include Unix, the Mac \s-1OS\s0, Plan 9, and Inferno..Sh "File Locking".IX Subsection "File Locking"In a multitasking environment, you may need to be careful not to collidewith other processes who want to do I/O on the same files as youare working on.  You'll often need shared or exclusive lockson files for reading and writing respectively.  You might justpretend that only exclusive locks exist..PPNever use the existence of a file \f(CW\*(C`\-e $file\*(C'\fR as a locking indication,because there is a race condition between the test for the existence ofthe file and its creation.  It's possible for another process to createa file in the slice of time between your existence check and your attemptto create the file.  Atomicity is critical..PPPerl's most portable locking interface is via the \f(CW\*(C`flock\*(C'\fR function,whose simplicity is emulated on systems that don't directly support itsuch as SysV or Windows.  The underlying semantics may affect howit all works, so you should learn how \f(CW\*(C`flock\*(C'\fR is implemented on yoursystem's port of Perl..PPFile locking \fIdoes not\fR lock out another process that would like todo I/O.  A file lock only locks out others trying to get a lock, notprocesses trying to do I/O.  Because locks are advisory, if one processuses locking and another doesn't, all bets are off..PPBy default, the \f(CW\*(C`flock\*(C'\fR call will block until a lock is granted.A request for a shared lock will be granted as soon as there is noexclusive locker.  A request for an exclusive lock will be granted assoon as there is no locker of any kind.  Locks are on file descriptors,not file names.  You can't lock a file until you open it, and you can'thold on to a lock once the file has been closed..PPHere's how to get a blocking shared lock on a file, typically usedfor reading:.PP.Vb 5\&    use 5.004;\&    use Fcntl qw(:DEFAULT :flock);\&    open(FH, "< filename")  or die "can\*(Aqt open filename: $!";\&    flock(FH, LOCK_SH)      or die "can\*(Aqt lock filename: $!";\&    # now read from FH.Ve.PPYou can get a non-blocking lock by using \f(CW\*(C`LOCK_NB\*(C'\fR..PP.Vb 2\&    flock(FH, LOCK_SH | LOCK_NB)\&        or die "can\*(Aqt lock filename: $!";.Ve.PPThis can be useful for producing more user-friendly behaviour by warningif you're going to be blocking:.PP.Vb 10\&    use 5.004;\&    use Fcntl qw(:DEFAULT :flock);\&    open(FH, "< filename")  or die "can\*(Aqt open filename: $!";\&    unless (flock(FH, LOCK_SH | LOCK_NB)) {\&        $| = 1;\&        print "Waiting for lock...";\&        flock(FH, LOCK_SH)  or die "can\*(Aqt lock filename: $!";\&        print "got it.\en"\&    } \&    # now read from FH.Ve.PPTo get an exclusive lock, typically used for writing, you have to becareful.  We \f(CW\*(C`sysopen\*(C'\fR the file so it can be locked before it getsemptied.  You can get a nonblocking version using \f(CW\*(C`LOCK_EX | LOCK_NB\*(C'\fR..PP.Vb 9\&    use 5.004;\&    use Fcntl qw(:DEFAULT :flock);\&    sysopen(FH, "filename", O_WRONLY | O_CREAT)\&        or die "can\*(Aqt open filename: $!";\&    flock(FH, LOCK_EX)\&        or die "can\*(Aqt lock filename: $!";\&    truncate(FH, 0)\&        or die "can\*(Aqt truncate filename: $!";\&    # now write to FH.Ve.PPFinally, due to the uncounted millions who cannot be dissuaded fromwasting cycles on useless vanity devices called hit counters, here'show to increment a number in a file safely:.PP.Vb 1\&    use Fcntl qw(:DEFAULT :flock);\&\&    sysopen(FH, "numfile", O_RDWR | O_CREAT)\&        or die "can\*(Aqt open numfile: $!";\&    # autoflush FH\&    $ofh = select(FH); $| = 1; select ($ofh);\&    flock(FH, LOCK_EX)\&        or die "can\*(Aqt write\-lock numfile: $!";\&\&    $num = <FH> || 0;\&    seek(FH, 0, 0)\&        or die "can\*(Aqt rewind numfile : $!";\&    print FH $num+1, "\en"\&        or die "can\*(Aqt write numfile: $!";\&\&    truncate(FH, tell(FH))\&        or die "can\*(Aqt truncate numfile: $!";\&    close(FH)\&        or die "can\*(Aqt close numfile: $!";.Ve.Sh "\s-1IO\s0 Layers".IX Subsection "IO Layers"In Perl 5.8.0 a new I/O framework called \*(L"PerlIO\*(R" was introduced.This is a new \*(L"plumbing\*(R" for all the I/O happening in Perl; for themost part everything will work just as it did, but PerlIO also broughtin some new features such as the ability to think of I/O as \*(L"layers\*(R".One I/O layer may in addition to just moving the data also dotransformations on the data.  Such transformations may includecompression and decompression, encryption and decryption, and transformingbetween various character encodings..PPFull discussion about the features of PerlIO is out of scope for thistutorial, but here is how to recognize the layers being used:.IP "\(bu" 4The three\-(or more)\-argument form of \f(CW\*(C`open\*(C'\fR is being used and thesecond argument contains something else in addition to the usual\&\f(CW\*(Aq<\*(Aq\fR, \f(CW\*(Aq>\*(Aq\fR, \f(CW\*(Aq>>\*(Aq\fR, \f(CW\*(Aq|\*(Aq\fR and their variants,for example:.Sp.Vb 1\&    open(my $fh, "<:crlf", $fn);.Ve.IP "\(bu" 4The two-argument form of \f(CW\*(C`binmode\*(C'\fR is being used, for example.Sp.Vb 1\&    binmode($fh, ":encoding(utf16)");.Ve.PPFor more detailed discussion about PerlIO see PerlIO;for more detailed discussion about Unicode and I/O see perluniintro..SH "SEE ALSO".IX Header "SEE ALSO"The \f(CW\*(C`open\*(C'\fR and \f(CW\*(C`sysopen\*(C'\fR functions in \fIperlfunc\fR\|(1);the system \fIopen\fR\|(2), \fIdup\fR\|(2), \fIfopen\fR\|(3), and \fIfdopen\fR\|(3) manpages;the \s-1POSIX\s0 documentation..SH "AUTHOR and COPYRIGHT".IX Header "AUTHOR and COPYRIGHT"Copyright 1998 Tom Christiansen..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 these files arehereby placed into the public domain.  You are permitted andencouraged to use this code in your own programs for fun or for profitas you see fit.  A simple comment in the code giving credit would becourteous but is not required..SH "HISTORY".IX Header "HISTORY"First release: Sat Jan  9 08:09:11 \s-1MST\s0 1999

⌨️ 快捷键说明

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