1 package Devel::REPL::Script;
7 use Module::Runtime 'use_module';
8 use namespace::autoclean;
12 with 'MooseX::Getopt';
15 is => 'ro', isa => 'Str',
16 default => sub { 'repl.rc' },
22 default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Default' },
26 is => 'ro', isa => 'Devel::REPL',
27 default => sub { Devel::REPL->new() }
32 $self->load_profile($self->profile);
33 $self->load_rcfile($self->rcfile);
37 my ($self, $profile) = @_;
38 $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
40 confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
41 unless $profile->does('Devel::REPL::Profile');
42 $profile->new->apply_profile($self->_repl);
46 my ($self, $rc_file) = @_;
48 # plain name => ~/.re.pl/${rc_file}
49 if ($rc_file !~ m!/!) {
50 $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
53 $self->apply_script($rc_file);
57 my ($self, $script, $warn_on_unreadable) = @_;
60 warn "File '$script' does not exist" if $warn_on_unreadable;
64 warn "File '$script' is unreadable" if $warn_on_unreadable;
68 open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
70 { local $/; $rc_data = <RCFILE>; }
71 close RCFILE; # Don't care if this fails
72 $self->eval_script($rc_data);
73 warn "Error executing script ${script}: $@\n" if $@;
77 my ($self, $data) = @_;
78 local $CURRENT_SCRIPT = $self;
79 $self->_repl->eval($data);
88 my ($class, @opts) = @_;
89 return unless (@opts == 1 && $opts[0] eq 'run');
90 $class->new_with_options->run;
94 confess "->current should only be called as class method" if ref($_[0]);
95 confess "No current instance (valid only during rc parse)"
96 unless $CURRENT_SCRIPT;
97 return $CURRENT_SCRIPT;