1 package Devel::REPL::Script;
7 use Module::Runtime 'use_module';
8 use vars qw($CURRENT_SCRIPT);
9 use namespace::autoclean;
11 with 'MooseX::Getopt';
14 is => 'ro', isa => 'Str',
15 default => sub { 'repl.rc' },
21 default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Default' },
25 is => 'ro', isa => 'Devel::REPL',
26 default => sub { Devel::REPL->new() }
31 $self->load_profile($self->profile);
32 $self->load_rcfile($self->rcfile);
36 my ($self, $profile) = @_;
37 $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
39 confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
40 unless $profile->does('Devel::REPL::Profile');
41 $profile->new->apply_profile($self->_repl);
45 my ($self, $rc_file) = @_;
47 # plain name => ~/.re.pl/${rc_file}
48 if ($rc_file !~ m!/!) {
49 $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
52 $self->apply_script($rc_file);
56 my ($self, $script, $warn_on_unreadable) = @_;
59 warn "File '$script' does not exist" if $warn_on_unreadable;
63 warn "File '$script' is unreadable" if $warn_on_unreadable;
67 open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
69 { local $/; $rc_data = <RCFILE>; }
70 close RCFILE; # Don't care if this fails
71 $self->eval_script($rc_data);
72 warn "Error executing script ${script}: $@\n" if $@;
76 my ($self, $data) = @_;
77 local $CURRENT_SCRIPT = $self;
78 $self->_repl->eval($data);
87 my ($class, @opts) = @_;
88 return unless (@opts == 1 && $opts[0] eq 'run');
89 $class->new_with_options->run;
93 confess "->current should only be called as class method" if ref($_[0]);
94 confess "No current instance (valid only during rc parse)"
95 unless $CURRENT_SCRIPT;
96 return $CURRENT_SCRIPT;