1 package Devel::REPL::Script;
7 use vars qw($CURRENT_SCRIPT);
8 use namespace::clean -except => [ qw(meta) ];
10 with 'MooseX::Getopt';
13 is => 'ro', isa => 'Str', required => 1, default => sub { 'repl.rc' },
20 default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Default' },
24 is => 'ro', isa => 'Devel::REPL', required => 1,
25 default => sub { Devel::REPL->new() }
30 $self->load_profile($self->profile);
31 $self->load_rcfile($self->rcfile);
35 my ($self, $profile) = @_;
36 $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
37 Class::MOP::load_class($profile);
38 confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
39 unless $profile->does('Devel::REPL::Profile');
40 $profile->new->apply_profile($self->_repl);
44 my ($self, $rc_file) = @_;
46 # plain name => ~/.re.pl/${rc_file}
47 if ($rc_file !~ m!/!) {
48 $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
51 $self->apply_script($rc_file);
55 my ($self, $script, $warn_on_unreadable) = @_;
58 warn "File '$script' does not exist" if $warn_on_unreadable;
62 warn "File '$script' is unreadable" if $warn_on_unreadable;
66 open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
68 { local $/; $rc_data = <RCFILE>; }
69 close RCFILE; # Don't care if this fails
70 $self->eval_script($rc_data);
71 warn "Error executing script ${script}: $@\n" if $@;
75 my ($self, $data) = @_;
76 local $CURRENT_SCRIPT = $self;
77 $self->_repl->eval($data);
86 my ($class, @opts) = @_;
87 return unless (@opts == 1 && $opts[0] eq 'run');
88 $class->new_with_options->run;
92 confess "->current should only be called as class method" if ref($_[0]);
93 confess "No current instance (valid only during rc parse)"
94 unless $CURRENT_SCRIPT;
95 return $CURRENT_SCRIPT;