thezip has asked for the wisdom of the Perl Monks concerning the following question:
I have a question pertaining to File::Find, but more generally about callbacks.
How can I pass the $days_ago parameter into the callback function "&findNewerThan"? I seem to lose the value of File::Find::name when I pass in a parameter...
Eventually, I'll do a stat on the current file to get the modification time, and then accept or reject the filename
I'll also have other callback functions, some which accept parameters, and others that don't. I'll use a hash of coderefs for this
Thanks in advance for your help!
#!/usr/bin/perl -w use strict; use Data::Dumper; use File::Find; # Global array for the accumulated files... my @files = (); # Any directory will suffice here... my $file_dir_root = "/var/log"; #------------------------------------------------ sub findNewerThan { #------------------------------------------------ # my $days_ago = shift; my $filename = $File::Find::name; return unless -f $filename; # Now stat the file to compare its modification time # with the days_ago parameter... push(@files, $filename); } #------------------------------------------------ sub days($) { #------------------------------------------------ my $days = shift; my $oneday = 60 * 60 * 24; return $days * $oneday; } #------------------------------------------------ # MAIN #------------------------------------------------ my $func = \&findNewerThan; #find($func->(days(5)), ($file_dir_root) ); find($func, ($file_dir_root) ); print Dumper(\@files);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a parameter to a callback
by ikegami (Patriarch) on Jul 19, 2006 at 21:24 UTC | |
|
Re: Passing a parameter to a callback
by runrig (Abbot) on Jul 19, 2006 at 21:36 UTC | |
|
Re: Passing a parameter to a callback
by diotalevi (Canon) on Jul 19, 2006 at 21:33 UTC | |
by thezip (Vicar) on Jul 19, 2006 at 21:47 UTC | |
by runrig (Abbot) on Jul 19, 2006 at 22:02 UTC | |
by diotalevi (Canon) on Jul 19, 2006 at 21:48 UTC | |
by thezip (Vicar) on Jul 19, 2006 at 21:58 UTC | |
|