in reply to glob directory to array, then wait, and check again
Hello flieckster,
You need to loop; for example:
use strict; use warnings; use POSIX qw( strftime ); my $dir = $ARGV[0] // '.'; my $wait = $ARGV[1] // 10; chdir $dir or die "$!"; my $count = () = glob '*'; print_dir_status($count); if ($count > 0) { my $equal = 0; until ($equal) { sleep $wait; my $new_count = () = glob '*'; $equal = 1 if $new_count == $count; $count = $new_count; print_dir_status($count); } } sub print_dir_status { my ($count) = @_; my $time = strftime "%a %d %b %Y %I:%M:%S %p", localtime; print "$time: there are $count files\n"; }
Update: Note that (contrary to the node title) it’s not necessary to store the results of the glob in an array. The syntax $count = () = glob '*'; evaluates glob in list context (via the assignment to empty parentheses), then implicitly puts that list into scalar context (by assigning to a scalar variable) to get the list count.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: glob directory to array, then wait, and check again
by flieckster (Scribe) on Aug 20, 2017 at 01:20 UTC |