1 package Devel::REPL::Script;
7 use vars qw($CURRENT_SCRIPT);
10 use MooX::Types::MooseLike::Base qw(Str InstanceOf);
27 is => 'ro', isa => InstanceOf('Devel::REPL'), required => 1,
28 default => sub { Devel::REPL->new() }
31 sub new_with_options {
34 my $rcfile = 'repl.rc';
35 my $profile = $ENV{DEVEL_REPL_PROFILE} || 'Default';
37 'rcfile=s' => \$rcfile,
38 'profile=s' => \$profile,
40 $class->new(profile => $profile, rcfile => $rcfile);
45 $self->load_profile($self->profile);
46 $self->load_rcfile($self->rcfile);
50 my ($self, $profile) = @_;
51 $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/;
52 Module::Load::load($profile);
53 confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'"
54 unless $profile->does('Devel::REPL::Profile');
55 $profile->new->apply_profile($self->_repl);
59 my ($self, $rc_file) = @_;
61 # plain name => ~/.re.pl/${rc_file}
62 if ($rc_file !~ m!/!) {
63 $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file);
66 $self->apply_script($rc_file);
70 my ($self, $script, $warn_on_unreadable) = @_;
73 warn "File '$script' does not exist" if $warn_on_unreadable;
77 warn "File '$script' is unreadable" if $warn_on_unreadable;
81 open RCFILE, '<', $script or die "Couldn't open ${script}: $!";
83 { local $/; $rc_data = <RCFILE>; }
84 close RCFILE; # Don't care if this fails
85 $self->eval_script($rc_data);
86 warn "Error executing script ${script}: $@\n" if $@;
90 my ($self, $data) = @_;
91 local $CURRENT_SCRIPT = $self;
92 $self->_repl->eval($data);
101 my ($class, @opts) = @_;
102 return unless (@opts == 1 && $opts[0] eq 'run');
103 $class->new_with_options->run;
107 confess "->current should only be called as class method" if ref($_[0]);
108 confess "No current instance (valid only during rc parse)"
109 unless $CURRENT_SCRIPT;
110 return $CURRENT_SCRIPT;