X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FDevel-REPL.git;a=blobdiff_plain;f=lib%2FDevel%2FREPL%2FScript.pm;h=fe01d53b60114b613fb3c0d4d9d81f21c1a8434b;hp=bf36136466ef8f2e09a210fbe7218fbf03b8a962;hb=28e93f31852a563bc36e1fd58c28eb46beeec64d;hpb=3bcf4eb89e6fd4f7ae37fae50255a2b09e655677 diff --git a/lib/Devel/REPL/Script.pm b/lib/Devel/REPL/Script.pm index bf36136..fe01d53 100644 --- a/lib/Devel/REPL/Script.pm +++ b/lib/Devel/REPL/Script.pm @@ -4,21 +4,26 @@ use Moose; use Devel::REPL; use File::HomeDir; use File::Spec; -use vars qw($CURRENT_SCRIPT); -use namespace::clean -except => [ qw(meta) ]; +use Module::Runtime 'use_module'; +use namespace::autoclean; + +our $CURRENT_SCRIPT; with 'MooseX::Getopt'; has 'rcfile' => ( - is => 'ro', isa => 'Str', required => 1, default => sub { 'repl.rc' }, + is => 'ro', isa => 'Str', + default => sub { 'repl.rc' }, ); has 'profile' => ( - is => 'ro', isa => 'Str', required => 1, default => sub { 'Default' }, + is => 'ro', + isa => 'Str', + default => sub { $ENV{DEVEL_REPL_PROFILE} || 'Default' }, ); has '_repl' => ( - is => 'ro', isa => 'Devel::REPL', required => 1, + is => 'ro', isa => 'Devel::REPL', default => sub { Devel::REPL->new() } ); @@ -31,7 +36,7 @@ sub BUILD { sub load_profile { my ($self, $profile) = @_; $profile = "Devel::REPL::Profile::${profile}" unless $profile =~ /::/; - Class::MOP::load_class($profile); + use_module $profile; confess "Profile class ${profile} doesn't do 'Devel::REPL::Profile'" unless $profile->does('Devel::REPL::Profile'); $profile->new->apply_profile($self->_repl); @@ -45,17 +50,30 @@ sub load_rcfile { $rc_file = File::Spec->catfile(File::HomeDir->my_home, '.re.pl', $rc_file); } - if (-r $rc_file) { - open RCFILE, '<', $rc_file || die "Couldn't open ${rc_file}: $!"; - my $rc_data; - { local $/; $rc_data = ; } - close RCFILE; # Don't care if this fails - $self->eval_rcdata($rc_data); - warn "Error executing rc file ${rc_file}: $@\n" if $@; + $self->apply_script($rc_file); +} + +sub apply_script { + my ($self, $script, $warn_on_unreadable) = @_; + + if (!-e $script) { + warn "File '$script' does not exist" if $warn_on_unreadable; + return; + } + elsif (!-r _) { + warn "File '$script' is unreadable" if $warn_on_unreadable; + return; } + + open RCFILE, '<', $script or die "Couldn't open ${script}: $!"; + my $rc_data; + { local $/; $rc_data = ; } + close RCFILE; # Don't care if this fails + $self->eval_script($rc_data); + warn "Error executing script ${script}: $@\n" if $@; } -sub eval_rcdata { +sub eval_script { my ($self, $data) = @_; local $CURRENT_SCRIPT = $self; $self->_repl->eval($data);