Filesystem.pm 943B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package SugarTrail::Repo::Filesystem;
  2. # provide access to STM data
  3. # * init()
  4. # * load_text()
  5. # * load_vcs()
  6. use strict;
  7. use warnings;
  8. use parent 'SugarTrail::Repo';
  9. use Carp;
  10. # initialize repo
  11. sub new {
  12. my $class = shift;
  13. my $args = { @_ };
  14. exists($args->{$_}) or croak "missing mandatory parameter: $_"
  15. foreach (qw/root/);
  16. my $self = { %$args };
  17. unless (-d $args->{root}) {
  18. $self->{last_error} = "invalid repository root directory: "
  19. . $args->{root};
  20. carp $self->{last_error};
  21. }
  22. return bless $self, $class;
  23. }
  24. sub load_text {
  25. my $self = shift;
  26. my $args = { @_ };
  27. # load
  28. my $fn = $self->{root} . $args->{source};
  29. my $fh;
  30. unless (open $fh, "<", $fn) {
  31. $self->{last_error} = "cannot open master $fn for reading: $!";
  32. carp $self->{last_error};
  33. return;
  34. }
  35. my $text = join "", <$fh>;
  36. close $fh;
  37. return $text;
  38. }
  39. 1;