new.spirit

 view release on metacpan or  search on metacpan

bin/dbshell.pl  view on Meta::CPAN

	}

	print "\n" if $lf;

	my $get_line_cb;
	
	my $shell;
	my $initialize_history;
	my $term;
	
	my $rl_package;
	if ( -t STDIN ) {
		# we're connected to a TTY, so install
		# a Term::Readline input handler
		require "Term/ReadLine.pm";
		$term = new Term::ReadLine 'dbshell';
		
		$rl_package = $term->ReadLine;
		if ( $rl_package eq 'Term::ReadLine::Perl' ) {
			$readline::rl_completion_function = 'main::perl_rl_completion';
		} elsif ( $rl_package eq 'Term::ReadLine::Gnu' ) {
			my $attribs = $term->Attribs;
			$attribs->{'attempted_completion_function'} = undef;
			$attribs->{'completion_entry_function'} = undef;
			$attribs->{'list_completion_function'} = undef;
		}

		$get_line_cb = sub {
			print "\n" if $shell->{command_completed};
			$shell->{command_completed} = 0;
			my $line = $term->readline (
				$shell->{username}.'@'.
				$shell->{selected_db}."> "
			);
			print "> $line" if $opts_href->{e};
			add_line_to_history ($line) if $line;
			print "\n" if not defined $line;
			return $line;
		};
		
		$initialize_history = 1;
		
	} else {
		# no TTY, so simply read from STDIN without
		# giving a prompt
		$get_line_cb = sub {
			my $line = <STDIN>;
			print "> $line" if $opts_href->{e};
			return $line;
		}
	}
	
	$shell = new NewSpirit::SqlShell::Text (
		source      => $source,
		username    => $username,
		password    => $password,
		selected_db => $selected_db,
		autocommit  => 1,
		echo        => 0,
		get_line_cb => $get_line_cb,
		preference_file => home_dir()."/.dbshell_prefs"
	);

	$shell->{abort_mode} = $opts_href->{x};

	if ( $initialize_history ) {
		$shell->info ("Initializing command history");
		initialize_history (
			$term, $shell->get_preference('history_size')
		) 
	}

	if ( $rl_package eq 'Term::ReadLine::Stub' ) {
		$shell->info ("WARNING: Only stub ReadLine support available.",
	        	      "Install Term::ReadLine::Gnu or Term::ReadLine::Perl",
			      "to get enhanced ReadLine support!")
	} elsif ( $rl_package ) {
		$shell->info ("ReadLine support activated using module '$rl_package'");
	}

	$shell->loop;

	print "\n" if $opts_href->{e};

	$shell->error_summary if not $shell->{abort_mode} and
	                         $print_error_summary;

	exit $shell->has_errors;
}

sub perl_rl_completion {
	my ($input) = @_;
	return $input." ";
}

sub ask {
	my %par = @_;
	
	my $label     = $par{label};
	my $invisible = $par{invisible};
	
	if ( $invisible ) {
		eval "use Term::ReadKey";
		ReadMode(2);
	}
	
	print "$label: ";
	my $input = <STDIN>;
	chomp $input;
	
	if ( $invisible ) {
		ReadMode(0);
		print "\n";
	}
	
	return $input;
}

{
	my $file_not_writable_already_warned;
	sub add_line_to_history {



( run in 2.685 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )