]> gitweb @ CieloNegro.org - pci-nopaste.git/blob - lib/POE/Component/IRC/Plugin/NoPaste/StaticFile.pm
migrate from GNU arch to Git
[pci-nopaste.git] / lib / POE / Component / IRC / Plugin / NoPaste / StaticFile.pm
1 # -*- cperl -*-
2 # -----------------------------------------------------------------------------
3 package POE::Component::IRC::Plugin::NoPaste::StaticFile;
4 use strict;
5 use warnings;
6 use File::Spec;
7 use File::stat;
8 require Exporter;
9 our @ISA = qw(Exporter);
10 our @EXPORT = qw(&static_file);
11
12 # path => [lastmod, content]
13 my $TABLE = {};
14
15 sub static_file {
16     my ($path) = @_;
17     my $fullpath;
18
19     foreach my $inc (@INC) {
20         my $fpath = File::Spec->canonpath(
21             File::Spec->catdir(
22                 File::Spec->splitdir($inc),
23                 qw(POE Component IRC Plugin NoPaste),
24                 File::Spec->splitdir($path)));
25         
26         if (-r $fpath) {
27             $fullpath = $fpath;
28             last;
29         }
30     }
31     defined $fullpath or return undef;
32     
33     my $lastmod = stat($fullpath)->mtime;
34
35     my $entry = $TABLE->{$fullpath};
36     if (!$entry or $entry->[0] < $lastmod) {
37         local $/ = undef;
38         open my $fh, '<', $fullpath;
39         my $content = <$fh>;
40         close $fh;
41         
42         $entry = $TABLE->{$fullpath} = [$lastmod, $content];
43     }
44
45     return $entry->[1];
46 }
47
48 1;