Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Using Term::ReadLine and pasting input with tabs

by crux_capacitor (Initiate)
on Mar 22, 2016 at 17:49 UTC ( #1158521=perlquestion: print w/replies, xml ) Need Help??

crux_capacitor has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!

I've written a tool for use with the SGUIL IDS interface. The tool allows you to copy and paste an alert directly into the prompt, which the script then parses for relevant info, retrieves output for you, and then takes you back to the prompt for the next input. When copy and pasting from SGUIL, the fields are tab separated, which makes for very easy parsing, however I've been trying to use Term::ReadLine to allow for pressing the up-arrow to access input history, editing a previous input, etc. When using Term::ReadLine for this, I've gotten the history part to work, but when pasting an alert, all the tabs are removed and so all the fields are smashed together without even a single space in between. This makes it impossible to parse, and defeats the entire purpose.

I tried changing the EditingMode to 'vi', and this fixes the pasting problem, and allows me to see the input history, but editing doesn't work anymore (backspace, delete, left/right arrows don't work).

Does anyone know how to get this to work so that I can both paste a SGUIL alert and preserve tabs, AND have access to the history and editing previous inputs?

Here is my code so far:

Here is sample input, paste into the prompt: 169 sensor-eth1 10.185133100 2016-03-22 16:49:31 80 49883 6 ET INFO PDF Using CCITTFax Filter

#!/usr/bin/perl use strict; use warnings; #Term::ReadLine stuff use Term::ReadLine; #use Term::ReadLine::Perl5; use Term::ReadLine::readline; #&readline::rl_set('EditingMode', 'vi'); my $term = Term::ReadLine->new('Query'); my $attribs = $term->Attribs; $term->ornaments(0); my $query = ""; while (1) { #readline prompt. uncomment this and comment out the standard prom +pt to switch #my $prompt = "Enter query [$query] (+ to append, go to run query, + x to leave, h for help)> "; #my $mquery = $term->readline($prompt); #standard prompt print "\nEnter query [$query] (+ to append, go to run query, x to +leave, h for help)> "; chomp(my $mquery = <>); last if $mquery eq "x"; next if $mquery eq ""; if ($mquery =~ m/^.*....-..-.. ..:..:..\t\d{1,3}\..{1,3}\..{1,3}\. +.{1,3}\t\d+\t\d{1,3}\..{1,3}\..{1,3}\..{1,3}\t\d+\t\d+\t.*$/) { #parse values out of the pasted alert #regex above matches a SGUIL alert print "Looks good, I can parse this but no history, or up-arro +w!\n"; my @fields = split(/\t/, $mquery); $query = "$fields[3] $fields[4] $fields[6]"; } else { print "Looks bad, but I have history!\n"; $query = $mquery; } }

Replies are listed 'Best First'.
Re: Using Term::ReadLine and pasting input with tabs
by Anonymous Monk on Mar 22, 2016 at 22:30 UTC
    That's because of tab completion, you need to disable it. IIRC it's done by binding (rl_bind_key in GNU Readline) the tab char to a function that just inserts literal tab. I don't know what backend you're using, so read its documentation.

    P.S. I checked the doc for Term::ReadLine::Gnu, it seems it's something like $term->bind_key(ord "\t", 'tab-insert')

      It worked! Thank you! I'm using Term::ReadLine::readline, and so the line that fixed it was just this:
      &readline::rl_bind('tab', 'tab-insert');

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1158521]
Front-paged by GotToBTru
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2020-02-19 01:56 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (80 votes). Check out past polls.