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

Hi monks,

I have a simple console program that steps through a file of TAB-delimited data. For each line the program presents it and prompts the user to do something. One of the things that you can do is to edit the current line. In this case, I present the old line as PREPUT text in a editable prompt using Term::ReadLine.

My problem is with the TAB field delimiters, TAB is the Term::ReadLine completion character. Can I suppress this behavior? Alternatively, the documentation implies I can escape the TAB, but having tried several variations of CTRL-J and '\t' without success, I'm stumped.

Thanks,

Larry Barnett

  • Comment on TAB character as literal in editable prompt

Replies are listed 'Best First'.
Re: TAB character as literal in editable prompt
by TJPride (Pilgrim) on Dec 15, 2011 at 19:40 UTC
    Not that hard, probably:

    $_ = "alpha\tbravo\tcharlie\n"; print; s/\t/\\t/g; print; ### You'd have user edit it s/\\t/\t/g; print; ### Write back to file

      Hi TJPride,

      It works. Using Term::ReadLine, the first regular expression is unnecessary. This package appears to escape '\' characters as they are input without disturbing the rest of the string. I know this because the first version (which pre-escaped the TABs) also worked.

      I now have:

      #!/usr/bin/perl use File::Basename; use Term::ReadLine; my $inFile = $ARGV[0]; die "no inFile" unless $ARGV[0]; open (my $in, "<", $inFile) or die "Can't open $inFile for reading."; my $term = new Term::ReadLine(basename($0)); while(my $before = <$in>) { chomp($before); my $after = $before; # Not needed, unless you want to see the escapes. # $after =~ s/\t/\\t/g; for (;;) { print "BEFORE: $before\n"; $after = $term->readline("AFTER: ", $after); my $ans = $term->readline("Accept? (y/n) -> ", "y"); if ( $ans =~ /^[Yy]/ ) { $after =~ s/\\t/\t/g; print "AFTER: $after\n\n"; last; } } }

      Thanks!

      Larry