I occasionally need to evaluate statements in Reply (a REPL for Perl) that return values that take a long time to print—I sometimes find myself waiting several minutes for the prompt to return. In that time, I can't evaluate any more code.
For example, If I try to assign to a variable a large DNA sequence read from a FASTA file, I will have to wait while Reply prints the sequence.
To be clear—the problem I am encountering is that Reply can only print to the terminal so fast, and that means that I have to wait unnecessarily. I don't mind waiting for a statement that simply takes a long time to evaluate because, e.g., I am calling a sub that is carrying out a time-consuming operation.
Here's a Minimal Reproducible Example that you can test with Reply to see that you must wait a long time after running the second line.
sub long_return_value { return "ATCG" x 10000000; }
my $seq = long_return_value;
How can I suppress the output of a single statement so that I don't have to wait?
Clarification: Why don't I mind waiting in the case when the statement takes a long time to evaluate? Well, I usually want to use the result of evaluating the statement for my next statement(s).
CodePudding user response:
Try installing Async then start reply like this:
reply -MAsync
For your first command, type:
sub BG(&) { my $code = shift; Async->new( sub { print "Background job finished: ", &$code, "\n" } ); "Background job started"; }
Now if you have a job you know will be long running, type:
BG { your_code_here; ...; }
It will say "Background job started", and you'll immediately get your terminal back to type in. When the background job is finished, it will print to screen (possibly annoyingly interrupting what you're doing).
If you want a version with quieter output:
sub BGq(&) { my $code = shift; Async->new( sub { &$code } ); }
Note that the background code is executed in a forked process, so cannot affect Reply's global state.
CodePudding user response:
Reply only shows you the result of evaluating the last statement you give it, so one workaround is to add a return
statement after the statement that would return a value that takes a long time to print. e.g.,
long_return_value; return;