File: Cacheable.pm

package info (click to toggle)
libipc-pubsub-perl 0.29-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 176 kB
  • sloc: perl: 1,471; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 842 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package IPC::PubSub::Cacheable;
use strict;
use warnings;
use Scalar::Util qw( refaddr );

my %Cache;
sub new {
    my $class = shift;
    my $self  = bless(\@_, $class);
    $self->BUILD;
    return $self;
}

sub BUILD {
    my $self    = shift;
    $Cache{ refaddr($self) } ||= do {
        require "IPC/PubSub/Cache/$self->[0].pm";
        "IPC::PubSub::Cache::$self->[0]"->new(@{$self->[1]});
    };
}

sub AUTOLOAD {
    no strict 'refs';
    no warnings 'uninitialized';

    my $meth    = (substr(our $AUTOLOAD, rindex($AUTOLOAD, '::') + 2) || $AUTOLOAD);
    my $code    = sub {
        my $self    = shift;
        my $cache   = $self->BUILD;
        unshift @_, $cache;
        goto &{$cache->can($meth)};
    };
    *$meth = $code;
    goto &$code;
}

sub DESTROY {
    my $self = shift;
    delete $Cache{ refaddr($self) };
}

1;