While I like this idea, a couple of minor comments. First, there's an awful lot of shifting going on. For things like this, the topicaliser, $_, is very useful. So you'd be able to set things up as:

checkdir('/usr/tmp', sub { -w $_ }); checkdir('/usr/tmp', sub { -r $_ }); checkdir('/usr/tmp', sub { -r $_ and -w $_ });
At this point, I want to point out my second minor comment. This is what _ is for.
# now it's: checkdir('/usr/tmp', sub { -r $_ and -w _ }); checkdir('/usr/tmp', sub { !/\.\./ });
Again, my next minor comment: I'm not sure what this is supposed to be checking. I'm guessing you're looking for a directory named ".." somewhere in there. However, I'm guessing that "/usr/blah..foo" should be permitted? Regexes are powerful, but with power comes great respon... er, complexity ;-) Rather than use a regex, I would actually suggest doing exactly what a human would do: split the path elements up, and then look that none of them are equal to "..". If that's what you really want. (On unix, I claim that this is almost never what you want, due to symlinks.)
use List::MoreUtils qw/none/; use File::Spec; checkdir('/usr/tmp', sub { none { $_ eq '..' } File::Spec->splitdir($_) });
With those minor comments, here's the minor change to checkDir:
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 (! do { local $_ = $path; $perm->() }) { die LOG ("Test failed on $path, not able to use it.", 0); } LOG ("Using $path as output directory...", 2); }
A bit more convoluted internally, but I think a much more perlish exterior.


In reply to Re^2: how to pass operators as arguments to a sub by Tanktalus
in thread how to pass operators as arguments to a sub by kurreburre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.