tags:

views:

92

answers:

1

This is for debugging purpose. I've got a for loop that generates some output to Cygwin bash's CLI.

I know that I can redirect outputs to text files and use Perl or even just a normal text editor to inspect the values printed out for a particular iteration, just feel that a bit painful.

What I am now thinking is, to place a special subroutine inside the for loop, so that it would be "interrupted" at the beginning of each iteration, and Perl script should only resume to run after user/programmer hits a key(the Enter Key from keyboard?)

In this way I can directly inspect the values printed out during each iteration.

Is there a simple way to do this, without using additional libraries/CPAN?

+3  A: 

If you just want to pause for user input each iteration, then just do a read from STDIN inside your loop:

my $debug = 1;
while ($looping) {
    if ( $debug ) {
        print "Press enter to continue\n";
        $input = <>;
    }
}

If the debug flag is set, then the program will pause until the user presses enter, then the loop will resume. You can print your variables before the pause. Set $debug = 0 to disable the pause.

ire_and_curses
@ire_and_curses : Thanks for your anwser, that works :)
Michael Mao
unless you call the script with an argument.
sid_com
@sid_com: Good point - I'd forgotten about that. If you haven't already extracted the contents of `@ARGV`, then yes, you're quite right. I don't normally see this behaviour myself because parsing command line args is usually the first thing I do in any code I write. If the `@ARGV` list is empty, the diamond operator points at STDIN.
ire_and_curses
@ire_and_curses : That's right. I've noticed that. And I just explicitly put STDIN inside the diamond operator, your logic definitely works :)
Michael Mao