# Open Source Code License -> https://choosealicense.com/licenses/isc/
#
package CNFGlobalFile;
-use strict; use warnings; no warnings qw(experimental::signatures); use feature qw(signatures say);
+use strict; use warnings;
+no warnings qw(experimental::signatures); use feature qw(signatures say);
-sub new ($class, $path) {
- my @stat = stat($path);
- bless {path=>$path, content_length => $stat[7], last_modified => $stat[9]}, $class;
+sub new {
+ my($class, $path) =@_;
+ my @stat = stat($path);
+ my $self = {path=>$path, content_length => $stat[7], last_modified => $stat[9]};
+ bless $self, $class;
+ return $self;
}
sub content($self) {
- return _load($self) -> {content} ;
+ return exists $self -> {content} ? $self -> {content} : load($self) -> {content} ;
}
sub changed($self){ return 1 if not $self->{last_modified}; #We return changed if doesn't exist.
my @stats = stat($self->{path});
$self -> {last_modified} = $stat[9];
return $self;
}
-sub _load($self){
- open(my $fh,'<:encoding(UTF-8)', $self->{path}) or die "Path: ".$self->{path}." not found!";
- read $fh, my $content, -s $fh;
- close $fh;
+sub load($self){
+ open(my $fh,'<:encoding(UTF-8)', $self->{path}) or die "Path: ".$self->{path}." not found!";
+ read $fh, my $content, -s $fh;
+ close $fh;
+
my @stat = stat($self->{path});
$self -> {content_date} = time;
$self -> {content_length} = $stat[7];
- $self -> {content} = \$content;
+ $self -> {content} = \$content if $content;
return $self;
}