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

hello monks!

as I mentioned before I'm writing a shell problem with tab completion capabilities using Term::ReadLine. my code is something like that:
use Term::ReadLine; my $term = new Term::ReadLine 'Command Line Interface'; my $attribs = $term->Attribs; $attribs->{completion_entry_function} = $attribs->{'list_completion_fu +nction'}; $attribs->{attempted_completion_function} = \&tabCompletionFunction; sub tabCompletionFunction { my ($text, $line, $start, $end) = @_; if ($somthing) { &printSomething(); print "\nPrompt> $line"; $attribs->{completion_word} = []; } else { # other thing $attribs->{completion_word} = &getTabCompletionArray($node); # g +et completions array } return $term->completion_matches($text, $attribs->{list_completion_f +unction}); }
The above function is suppose to get the array for the tab completion in case 'other thing' happens. if 'something' happens I want to print something and return the prompt with no completion (therefore $atrribs->{completion_word} is set to an empty array).
the problem is that if 'somthing' happens then the function printSomething() is called and the print in the next line (print "\nPrompt> $line";) is not printed to screen, it is printed only the next time the user presses the tab key. a little testing reveals that if I'll add "\n" after printing the prompt line, this line will be printed on the right tab pressing, but then the user will enter his input on an empty line and not in the same line of the prompt. I'v tried everything already and couldn't find a solution, anyone?

Thanks in advance

Hotshot

Replies are listed 'Best First'.
Re: problem with tab completion
by pg (Canon) on Dec 12, 2002 at 16:01 UTC
    Just flush your output. The reason that "\n" made it printed, is because "\n" cause a flush of output immediately.

    Update: Add $| ++ at the beginning of your code.
    This prints nothing: print "a"; while (1) {} This prints a: print "a\n"; while (1) {} This also prints a: $| ++; print "a"; while (1) {}
      pg said: Just flush your output.

      How do I do that?

      Hotshot

        Put

        $|++;

        near the top of your program. See perlop and search for "$AUTOFLUSH" for the details.


        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.