File Coverage

blib/lib/Devel/REPL/Script.pm
Criterion Covered Total %
statement 18 55 32.7
branch 0 24 0.0
condition 0 3 0.0
subroutine 6 14 42.8
pod 0 7 0.0
total 24 103 23.3


line stmt bran cond sub pod time code
1             package Devel::REPL::Script;
2              
3             our $VERSION = '1.003028';
4              
5 1     1   789 use Moose;
  1         1  
  1         5  
6 1     1   3937 use Devel::REPL;
  1         2  
  1         19  
7 1     1   441 use File::HomeDir;
  1         3598  
  1         51  
8 1     1   4 use File::Spec;
  1         1  
  1         16  
9 1     1   17 use Module::Runtime 'use_module';
  1         1  
  1         6  
10 1     1   32 use namespace::autoclean;
  1         1  
  1         6  
11              
12             our $CURRENT_SCRIPT;
13              
14             with 'MooseX::Getopt';
15              
16             has 'rcfile' => (
17             is => 'ro', isa => 'Str',
18             default => sub { 'repl.rc' },
19             );
20              
21             has 'profile' => (
22             is => 'ro',
23             isa => 'Str',
24             default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Minimal' },
25             );
26              
27             has '_repl' => (
28             is => 'ro', isa => 'Devel::REPL',
29             default => sub { Devel::REPL->new() }
30             );
31              
32             sub BUILD {
33 0     0 0   my ($self) = @_;
34 0           $self->load_profile($self->profile);
35 0           $self->load_rcfile($self->rcfile);
36             }
37              
38             sub load_profile {
39 0     0 0   my ($self, $profile) = @_;
40 0 0         $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
41 0           use_module $profile;
42 0 0         confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
43             unless $profile->does('Devel::REPL::Profile');
44 0           $profile->new->apply_profile($self->_repl);
45             }
46              
47             sub load_rcfile {
48 0     0 0   my ($self, $rc_file) = @_;
49              
50             # plain name => ~/.re.pl/${rc_file}
51 0 0         if ($rc_file !~ m!/!) {
52 0           $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
53             }
54              
55 0           $self->apply_script($rc_file);
56             }
57              
58             sub apply_script {
59 0     0 0   my ($self, $script, $warn_on_unreadable) = @_;
60              
61 0 0         if (!-e $script) {
    0          
62 0 0         warn "File '$script' does not exist" if $warn_on_unreadable;
63 0           return;
64             }
65             elsif (!-r _) {
66 0 0         warn "File '$script' is unreadable" if $warn_on_unreadable;
67 0           return;
68             }
69              
70 0 0         open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
71 0           my $rc_data;
72 0           { local $/; $rc_data = <RCFILE>; }
  0            
  0            
73 0           close RCFILE; # Don't care if this fails
74 0           $self->eval_script($rc_data);
75 0 0         warn "Error executing script ${script}: $@\n" if $@;
76             }
77              
78             sub eval_script {
79 0     0 0   my ($self, $data) = @_;
80 0           local $CURRENT_SCRIPT = $self;
81 0           $self->_repl->eval($data);
82             }
83              
84             sub run {
85 0     0 0   my ($self) = @_;
86 0           $self->_repl->run;
87             }
88              
89             sub import {
90 0     0     my ($class, @opts) = @_;
91 0 0 0       return unless (@opts == 1 && $opts[0] eq 'run');
92 0           $class->new_with_options->run;
93             }
94              
95             sub current {
96 0 0   0 0   confess "->current should only be called as class method" if ref($_[0]);
97 0 0         confess "No current instance (valid only during rc parse)"
98             unless $CURRENT_SCRIPT;
99 0           return $CURRENT_SCRIPT;
100             }
101              
102             1;