App-Prima-REPL

 view release on metacpan or  search on metacpan

bin/prima-repl  view on Meta::CPAN

# Override PDL::Graphics::Prima::Simple::plot
if ($loaded_Prima_Graphics) {
	no warnings qw(redefine once);

	*PDL::Graphics::Prima::Simple::plot = sub {
		# Make sure PDL::Graphics::Prima is loaded and they provided good arguments
		return REPL::warn "PDL::Graphics::Prima did not load successfully!"
			if not $loaded_Prima_Graphics;
		return REPL::warn "prima_plot expects a collection of key => value pairs, but you sent"
			. " an odd number of arguments!" if @_ % 2 == 1;
		
		# Get the plotting arguments and supply a meaningful default pack:
		my %args = (
			pack => { fill => 'both', expand => 1},
			@_,
		);
		
		# Create the plotter, go to the tab, and return the plotter
		my $plotter;
		if ($REPL::emulate_simple) {
			$plotter = Prima::Window->create(
				text  => $args{title} || 'PDL::Graphics::Prima',
				size  => $args{size} || [@REPL::default_sizes],
			)->insert('Plot',
				pack => { fill => 'both', expand => 1},
				%args
			);
		}
		else {
			# Figure out the plot name:
			my $name = $args{title} || 'Plot';
			# Build the plot tab and switch to it:
			$plotter = REPL::create_new_tab($name, Plot => %args);
			REPL::goto_page -1;
		}
		return $plotter;
	};
	
	*main::plot = \&PDL::Graphics::Prima::Simple::plot;
}

################################################################################
#                                Handling Evals                                #
################################################################################

$inline->add_notification(Evaluate => sub {
	main::my_eval($_[1]);
});

=begin consideration

# I used to issue warnings when I found 'my' in the text to be eval'd. This was
# a means to allow for such lexical variables, but I've decided to not even
# worry about it.
#my $lexicals_allowed = 0;
#sub allow_lexicals { $lexicals_allowed = 1 };
	else {
		# A command to be eval'd. Lexical variables don't work, so croak if I
		# see one. This could probably be handled better.
		if ($in_text =~ /my/ and not $lexicals_allowed) {
			$@ = join(' ', 'It looks to me like you\'re trying to use a lexical variable.'
					, 'Lexical variables not allowed in the line evaluator'
					, 'because you cannot get to them after the current line.'
					, 'If I\'m wrong, or if you really want to use lexical variables,'
					, "do this:\n"
					, "   allow_lexicals; <command-here>"
					);
		}
		else {
			my $text_to_eval = $in_text;
			# This appears to be giving trouble. Slices do not appear to be
			# evaluated correctly. working here
			$text_to_eval = PDL::NiceSlice->perldlpp($in_text) if ($loaded_PDL);
			main::my_eval($text_to_eval);
		}
	
		# If error, print that to the output
		if ($@) {
			REPL::warn($@);
			$@ = '';
		}
	}
	$lexicals_allowed = 0
	
});

=end consideration

=cut

###############################################
# Various API and useful function definitions #
###############################################

package main;
#my $eval_container = Eval::WithLexicals->new;

sub my_eval {
	my $text = shift;
	# Gray the line entry:
	$REPL::inline->enabled(0);
	# replace the entry text with the text 'working...' and save the old stuff
	my $old_text = $REPL::inline->text;
	$REPL::inline->text('working ...');
	
	# Process the text with NiceSlice if they try to use it:
	if ($text =~ /use PDL::NiceSlice/) {
		if ($loaded_PDL) {
			$text = PDL::NiceSlice->perldlpp($text);
		}
		else {
			REPL::warn("PDL did not load properly, so I can't apply NiceSlice to your code.\n",
				"Don't be surprised if you get errors...\n");
		}
	}
	
	# Make sure any updates hit the screen before we get going:
	$::application->yield;
	# Run the stuff to be run:
	no strict;
#	eval { $eval_container->eval($text) };



( run in 0.737 second using v1.01-cache-2.11-cpan-39bf76dae61 )