in reply to how to stop command

my $file; my @file_list; foreach $file(@file_list){ print "$file\n"; }

This will achieve nothing, ever. @file_list is empty so the loop will not be entered. Is this the source of your confusion? If so, declare @file_list before the eval instead and then don't re-declare it inside.

Replies are listed 'Best First'.
Re^2: how to stop command
by dideod.yang (Sexton) on Jul 22, 2018 at 10:29 UTC
    Hi :) you mean below script?
    my $timeout = 5; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; my @file_list = `ls user/test/`; my $file; my @file_list; foreach $file(@file_list){ print "$file\n"; } alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # timed out } else { # didn't }

      Not quite. I mean more like this:

      my $timeout = 5; my @file_list; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; @file_list = `ls user/test/`; alarm 0; }; if ($@) { die $@ unless $@ eq "alarm\n"; # propagate unexpected errors # timed out print "Have incomplete file list\n"; } else { print "Have complete file list\n"; } foreach my $file (@file_list) { print "$file\n"; }