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

📄 file.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 5 页
字号:
  $self->[$i] = $item;  $self->[0][1]->_heap_move($self->[$i][KEY], $i);  $self->_nelts_inc;}# Remove the item at node $i from the heap, moving child items upwards.# The item with the smallest sequence number is always at the top.# Moving items upwards maintains this condition.# Return the removed item.  Return undef if there was no item at node $i.sub remove {  my ($self, $i) = @_;  $i = 1 unless defined $i;  my $top = $self->[$i];  return unless defined $top;  while (1) {    my $ii;    my ($L, $R) = (2*$i, 2*$i+1);    # If either is undefined, go the other way.    # Otherwise, go towards the smallest.    last unless defined $self->[$L] || defined $self->[$R];    $ii = $R if not defined $self->[$L];    $ii = $L if not defined $self->[$R];    unless (defined $ii) {      $ii = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;    }    $self->[$i] = $self->[$ii]; # Promote child to fill vacated spot    $self->[0][1]->_heap_move($self->[$i][KEY], $i);    $i = $ii; # Fill new vacated spot  }  $self->[0][1]->_heap_move($top->[KEY], undef);  undef $self->[$i];  $self->_nelts_dec;  return $top->[DAT];}sub popheap {  my $self = shift;  $self->remove(1);}# set the sequence number of the indicated item to a higher number# than any other item in the heap, and bubble the item down to the# bottom.sub promote {  my ($self, $n) = @_;#  $self->_check_loc($n);  $self->[$n][SEQ] = $self->_nseq;  my $i = $n;  while (1) {    my ($L, $R) = (2*$i, 2*$i+1);    my $dir;    last unless defined $self->[$L] || defined $self->[$R];    $dir = $R unless defined $self->[$L];    $dir = $L unless defined $self->[$R];    unless (defined $dir) {      $dir = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;    }    @{$self}[$i, $dir] = @{$self}[$dir, $i];    for ($i, $dir) {      $self->[0][1]->_heap_move($self->[$_][KEY], $_) if defined $self->[$_];    }    $i = $dir;  }}# Return item $n from the heap, promoting its LRU statussub lookup {  my ($self, $n) = @_;#  $self->_check_loc($n);  my $val = $self->[$n];  $self->promote($n);  $val->[DAT];}# Assign a new value for node $n, promoting it to the bottom of the heapsub set_val {  my ($self, $n, $val) = @_;#  $self->_check_loc($n);  my $oval = $self->[$n][DAT];  $self->[$n][DAT] = $val;  $self->promote($n);  return $oval;}# The hask key has changed for an item;# alter the heap's record of the hash keysub rekey {  my ($self, $n, $new_key) = @_;#  $self->_check_loc($n);  $self->[$n][KEY] = $new_key;}sub _check_loc {  my ($self, $n) = @_;  unless (1 || defined $self->[$n]) {    confess "_check_loc($n) failed";  }}BEGIN { *_ci_warn = \&Tie::File::_ci_warn }sub _check_integrity {  my $self = shift;  my $good = 1;  my %seq;  unless (eval {$self->[0][1]->isa("Tie::File::Cache")}) {    _ci_warn "Element 0 of heap corrupt";    $good = 0;  }  $good = 0 unless $self->_satisfies_heap_condition(1);  for my $i (2 .. $#{$self}) {    my $p = int($i/2);          # index of parent node    if (defined $self->[$i] && ! defined $self->[$p]) {      _ci_warn "Element $i of heap defined, but parent $p isn't";      $good = 0;    }    if (defined $self->[$i]) {      if ($seq{$self->[$i][SEQ]}) {        my $seq = $self->[$i][SEQ];        _ci_warn "Nodes $i and $seq{$seq} both have SEQ=$seq";        $good = 0;      } else {        $seq{$self->[$i][SEQ]} = $i;      }    }  }  return $good;}sub _satisfies_heap_condition {  my $self = shift;  my $n = shift || 1;  my $good = 1;  for (0, 1) {    my $c = $n*2 + $_;    next unless defined $self->[$c];    if ($self->[$n][SEQ] >= $self->[$c]) {      _ci_warn "Node $n of heap does not predate node $c";      $good = 0 ;    }    $good = 0 unless $self->_satisfies_heap_condition($c);  }  return $good;}# Return a list of all the values, sorted by expiration ordersub expire_order {  my $self = shift;  my @nodes = sort {$a->[SEQ] <=> $b->[SEQ]} $self->_nodes;  map { $_->[KEY] } @nodes;}sub _nodes {  my $self = shift;  my $i = shift || 1;  return unless defined $self->[$i];  ($self->[$i], $self->_nodes($i*2), $self->_nodes($i*2+1));}"Cogito, ergo sum.";  # don't forget to return a true value from the file__END__=head1 NAMETie::File - Access the lines of a disk file via a Perl array=head1 SYNOPSIS	# This file documents Tie::File version 0.97	use Tie::File;	tie @array, 'Tie::File', filename or die ...;	$array[13] = 'blah';     # line 13 of the file is now 'blah'	print $array[42];        # display line 42 of the file	$n_recs = @array;        # how many records are in the file?	$#array -= 2;            # chop two records off the end	for (@array) {	  s/PERL/Perl/g;         # Replace PERL with Perl everywhere in the file	}	# These are just like regular push, pop, unshift, shift, and splice	# Except that they modify the file in the way you would expect	push @array, new recs...;	my $r1 = pop @array;	unshift @array, new recs...;	my $r2 = shift @array;	@old_recs = splice @array, 3, 7, new recs...;	untie @array;            # all finished=head1 DESCRIPTIONC<Tie::File> represents a regular text file as a Perl array.  Eachelement in the array corresponds to a record in the file.  The firstline of the file is element 0 of the array; the second line is element1, and so on.The file is I<not> loaded into memory, so this will work even forgigantic files.Changes to the array are reflected in the file immediately.Lazy people and beginners may now stop reading the manual.=head2 C<recsep>What is a 'record'?  By default, the meaning is the same as for theC<E<lt>...E<gt>> operator: It's a string terminated by C<$/>, which isprobably C<"\n">.  (Minor exception: on DOS and Win32 systems, a'record' is a string terminated by C<"\r\n">.)  You may change thedefinition of "record" by supplying the C<recsep> option in the C<tie>call:	tie @array, 'Tie::File', $file, recsep => 'es';This says that records are delimited by the string C<es>.  If the filecontained the following data:	Curse these pesky flies!\nthen the C<@array> would appear to have four elements:	"Curse th"	"e p"	"ky fli"	"!\n"An undefined value is not permitted as a record separator.  Perl'sspecial "paragraph mode" semantics (E<agrave> la C<$/ = "">) are notemulated.Records read from the tied array do not have the record separatorstring on the end; this is to allow	$array[17] .= "extra";to work as expected.(See L<"autochomp">, below.)  Records stored into the array will havethe record separator string appended before they are written to thefile, if they don't have one already.  For example, if the recordseparator string is C<"\n">, then the following two lines do exactlythe same thing:	$array[17] = "Cherry pie";	$array[17] = "Cherry pie\n";The result is that the contents of line 17 of the file will bereplaced with "Cherry pie"; a newline character will separate line 17from line 18.  This means that this code will do nothing:	chomp $array[17];Because the C<chomp>ed value will have the separator reattached whenit is written back to the file.  There is no way to create a filewhose trailing record separator string is missing.Inserting records that I<contain> the record separator string is notsupported by this module.  It will probably produce a reasonableresult, but what this result will be may change in a future version.Use 'splice' to insert records or to replace one record with several.=head2 C<autochomp>Normally, array elements have the record separator removed, so that ifthe file contains the text	Gold	Frankincense	Myrrhthe tied array will appear to contain C<("Gold", "Frankincense","Myrrh")>.  If you set C<autochomp> to a false value, the recordseparator will not be removed.  If the file above was tied with	tie @gifts, "Tie::File", $gifts, autochomp => 0;then the array C<@gifts> would appear to contain C<("Gold\n","Frankincense\n", "Myrrh\n")>, or (on Win32 systems) C<("Gold\r\n","Frankincense\r\n", "Myrrh\r\n")>.=head2 C<mode>Normally, the specified file will be opened for read and write access,and will be created if it does not exist.  (That is, the flagsC<O_RDWR | O_CREAT> are supplied in the C<open> call.)  If you want tochange this, you may supply alternative flags in the C<mode> option.See L<Fcntl> for a listing of available flags.For example:	# open the file if it exists, but fail if it does not exist	use Fcntl 'O_RDWR';	tie @array, 'Tie::File', $file, mode => O_RDWR;	# create the file if it does not exist	use Fcntl 'O_RDWR', 'O_CREAT';	tie @array, 'Tie::File', $file, mode => O_RDWR | O_CREAT;	# open an existing file in read-only mode	use Fcntl 'O_RDONLY';	tie @array, 'Tie::File', $file, mode => O_RDONLY;Opening the data file in write-only or append mode is not supported.=head2 C<memory>This is an upper limit on the amount of memory that C<Tie::File> willconsume at any time while managing the file.  This is used for twothings: managing the I<read cache> and managing the I<deferred writebuffer>.Records read in from the file are cached, to avoid having to re-readthem repeatedly.  If you read the same record twice, the first time itwill be stored in memory, and the second time it will be fetched fromthe I<read cache>.  The amount of data in the read cache will notexceed the value you specified for C<memory>.  If C<Tie::File> wantsto cache a new record, but the read cache is full, it will make roomby expiring the least-recently visited records from the read cache.The default memory limit is 2Mib.  You can adjust the maximum readcache size by supplying the C<memory> option.  The argument is thedesired cache size, in bytes.	# I have a lot of memory, so use a large cache to speed up access	tie @array, 'Tie::File', $file, memory => 20_000_000;Setting the memory limit to 0 will inhibit caching; records will befetched from disk every time you examine them.The C<memory> value is not an absolute or exact limit on the memoryused.  C<Tie::File> objects contains some structures besides the readcache and the deferred write buffer, whose sizes are not chargedagainst C<memory>. The cache itself consumes about 310 bytes per cached record, so ifyour file has many short records, you may want to decrease the cachememory limit, or else the cache overhead may exceed the size of thecached data.=head2 C<dw_size>(This is an advanced feature.  Skip this section on first reading.)If you use deferred writing (See L<"Deferred Writing">, below) thendata you write into the array will not be written directly to thefile; instead, it will be saved in the I<deferred write buffer> to bewritten out later.  Data in the deferred write buffer is also chargedagainst the memory limit you set with the C<memory> option.You may set the C<dw_size> option to limit the amount of data that canbe saved in the deferred write buffer.  This limit may not exceed thetotal memory limit.  For example, if you set C<dw_size> to 1000 andC<memory> to 2500, that means that no more than 1000 bytes of deferredwrites will be saved up.  The space available for the read cache willvary, but it will always be at least 1500 bytes (if the deferred writebuffer is full) and it could grow as large as 2500 bytes (if thedeferred write buffer is empty.)If you don't specify a C<dw_size>, it defaults to the entire memorylimit.=head2 Option FormatC<-mode> is a synonym for C<mode>.  C<-recsep> is a synonym forC<recsep>.  C<-memory> is a synonym for C<memory>.  You get theidea.=head1 Public MethodsThe C<tie> call returns an object, say C<$o>.  You may call	$rec = $o->FETCH($n);	$o->STORE($n, $rec);to fetch or store the record at line C<$n>, respectively; similarlythe other tied array methods.  (See L<perltie> for details.)  You mayalso call the following methods on this object:=head2 C<flock>	$o->flock(MODE)will lock the tied file.  C<MODE> has the same meaning as the secondargument to the Perl built-in C<flock> function; for exampleC<LOCK_SH> or C<LOCK_EX | LOCK_NB>.  (These constants are provided bythe C<use Fcntl ':flock'> declaration.)C<MODE> is optional; the default is C<LOCK_EX>.C<Tie::File> maintains an internal table of the byte offset of eachrecord it has seen in the file.  When you use C<flock> to lock the file, C<Tie::File> assumes that theread cache is no longer trustworthy, because another process mighthave modified the file since the last time it was read.  Therefore, asuccessful call to C<flock> discards the contents of the read cacheand the internal record offset table.C<Tie::File> promises that the following sequence of operations willbe safe:	my $o = tie @array, "Tie::File", $filename;	$o->flock;In particular, C<Tie::File> will I<not> read or write the file duringthe C<tie> call.  (Exception: Using C<mode =E<gt> O_TRUNC> will, ofcourse, erase the file during the C<tie> call.  If you want to do thissafely, then open the file without C<O_TRUNC>, lock the file, and useC<@array = ()>.)The best way to unlock a file is to discard the object and untie thearray.  It is probably unsafe to unlock the file without also untyingit, because if you do, changes may remain unwritten inside the object.That is why there is no shortcut for unlocking.  If you really want tounlock the file prematurely, you know what to do; if you don't knowwhat to do, then don't do it.All the usual warnings about file locking apply here.  In particular,note that file locking in Perl is B<advisory>, which means thatholding a lock will not prevent anyone else from reading, writing, orerasing the file; it only prevents them from getting another lock atthe same time.  Locks are analogous to green traffic lights: If youhave a green light, that does not prevent the idiot coming the otherway from plowing into you sideways; it merely guarantees to you thatthe idiot does not also have a green light at the same time.=head2 C<autochomp>	my $old_value = $o->autochomp(0);    # disable autochomp option	my $old_value = $o->autochomp(1);    #  enable autochomp option	my $ac = $o->autochomp();   # recover current valueSee L<"autochomp">, above.=head2 C<defer>, C<flush>, C<discard>, and C<autodefer>See L<"Deferred Writing">, below.=head2 C<offset>	$off = $o->offset($n);This method returns the byte offset of the start of the C<$n>th recordin the file.  If there is no such re

⌨️ 快捷键说明

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