in reply to Re: Executing a subroutine in an IF
in thread Executing a subroutine in an IF

Hi morgon, Thank you for the answer. Reading the key works just fine now, but the sub still won't execute. I feel as if this should work .. read the key, if it's ctrl-r then execute the subroutine. Am I missing something crucial here? Regards, David

Replies are listed 'Best First'.
Re^3: Executing a subroutine in an IF
by david_l (Initiate) on Jun 16, 2012 at 08:47 UTC

    I feel kind of foolish now, I forgot to reset the $count scalar to 0 before calling the subroutine again. It works as intended now. :-)

    #!/bin/perl use strict; use warnings; use diagnostics; use Term::ANSIColor; use Term::ReadKey; use JSON; open(my $fh, '<', '/home/david/dev/linux.json'); my $json_text = <$fh>; my $test_scalar = decode_json($json_text); my $max = 10; my $count = 0; sub reddit { while ($count <= $max) { print colored ['red'], "-> $test_scalar->{data}{childr +en}[$count]{data}{title}\n"; print colored ['yellow'], "$test_scalar->{data}{childr +en}[$count]{data}{url}\n"; $count++; } } reddit(); ReadMode 4; my $wait = ReadKey(0); ReadMode 0; if ($wait eq "\cR") { $count = 0; reddit(); }