in reply to how to pass operators as arguments to a sub
Rather than passing an operator per se, I'd pass a code reference that uses the operator...
checkDir('/usr/tmp', sub { -w shift }); # need write permission. checkDir('/usr/tmp', sub { -r shift }); # need read permission. checkDir('/usr/tmp', sub { my $x = shift; -r $x and -w $x; }); # need +both. checkDir('/usr/tmp', sub { my $x = shift; not $x =~ /\.\./; }); # safe +ty check. sub checkDir { my ($path, $perm) = @_; ### Check if output dir exists if (! defined $path) { LOG ("Output path not defined; using temp instead...", 1); $path = '/var/tmp/'; } if (! -d $path) { die LOG ("$path is not a valid directory...", 0); } if (! $perm->($path)) { die LOG ("Test failed on $path, not able to use it.", 0); } LOG ("Using $path as output directory...", 2); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to pass operators as arguments to a sub
by Tanktalus (Canon) on May 03, 2006 at 17:42 UTC |