
Kanji
User
Aug 6, 2000, 6:52 PM
Post #8 of 10
(818 views)
|
|
Re: Limiting Items written to a File
[In reply to]
|
Can't Post
|
|
Playing heretic here, but why do you have to use a file? Why not a directory instead? <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> use URI::Escape; my $no = 5; # How many most recent to display my $ref_dir = "/path/to/ref_dir"; if ( opendir REFS, $ref_dir ) { my @refs; if ( chdir $ref_dir ) { while ( my $ref = readdir REFS ) { # Ignore the file if it doesn't start with something # protocol(ish) looking (ie, http%3A, etc.) next unless $ref =~ /^\w+:/; # %3A = : # Start builing a mapping of filenames and their last # modified (_NOT_ created!) dates. push @refs, sprintf( "%020d %s", -M $ref, $ref ); } # Do a partial Guttman-Rossler Transform to sort the # referrers by last modified date. @refs = map { ( split )[1] } reverse sort @refs; # Extract most recent refererrs. my @recent = map { uri_unescape($_) } splice( @refs, 0, $no ); # @recent now has the $no most recent referrers, while # @refs contains a list of any older referrers/files # for further processing, such as tidying up with: # unlink( @refs ) or warn( "..." ); print "The most recent referrers were:", map { qq(<a href="$_">$_</a><br> ) } @recent if @recent; # Finally, add the current visitor to the list of refs. # by escaping the referrer into a safe filename, and then # creating it if it doesn't exist or updating it if it # does. It can be empty for the purposes of this script. my $ref = uri_escape( $ENV{'HTTP_REFERER'}, "\\W.-:" ); unless ( open REF, "> $ref_dir/$ref" ) { warn( "open: $!" ); } } else { warn( "chdir: $!" ); } } else { warn( "opendir: $!" ); }</pre><HR></BLOCKQUOTE>
|