package cache;
use strict;
######################################################################
+########
sub new {
my $class=shift;
my $size=shift;
my $self=bless {};
$size=30 unless $size;
$self->{array}=[];
$self->{size}=$size;
$self;
}
sub ins {
my $self=shift;
my $id=shift;
my $data=shift;
my $array=$self->{array};
unshift @$array,[$id,$data];
if ($#{$array} > $self->{size}) {
pop @$array;
}
}
sub get {
my $self=shift;
my $id=shift;
my $array=$self->{array};
my $data;
for my $p (0..$#{$array}) {
if ($array->[$p]->[0] eq $id) {
$data=$array->[$p]->[1];
splice(@$array,$p,1);
unshift @$array,[$id,$data];
last;
}
}
return unless defined $data;
$data;
}
sub del {
my $self=shift;
my $id=shift;
my $array=$self->{array};
my $data;
for my $p (0..$#{$array}) {
if ($array->[$p]->[0] eq $id) {
splice(@$array,$p,1);
last;
}
}
}
sub show {
my $self=shift;
my $array=$self->{array};
local $_;
for (@$array) {
print "id $_->[0] data $_->[1]\n";
}
}
1;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|