Category Archives: Perl

Perl programming

Cache::FileCache Thoughts

If using perl Cache::FileCache, some comments offering a helpful starting point to get a cache that works in a relatively well behaved and unsurprising way:

use Cache::FileCache;
use File::Spec;

my $cache = Cache::FileCache->new({
    # "The namespace associated with this
    # cache."
    namespace => 'BJD-TESTING-app_id_cache_id',
    # "The default expiration time for
    # objects place in the cache."
    # This is in seconds.
    default_expires_in => 1,
    # "Sets the auto purge interval. If this
    # option is set to a particular time ( in
    # the same format as the expires_in ),
    # then the purge( ) routine will be
    # called during the first set after the
    # interval expires. The interval will
    # then be reset."
    # XXX To work needs at least one of
    # auto_purge_on_set or auto_purge_on_get
    # to be set to true
    auto_purge_interval => 5,
    auto_purge_on_set => 1,
    # restrict access to the cache to just this
    # user (data security)
    # NOTE that if you set directory_umask
    # but no cache_root you can end up clashing
    # with other users who will also be trying
    # to use (and maybe set the umask) on
    # /tmp/FileCache
    # If we care about data security set
    # cache_root as well to put the files in
    # out own private directory:
    directory_umask => 0077,
    cache_root =>
      . "/bjdean-perl-Cache-FileCache",

Perl Gotcha : xor precedence

Perl has a logical xor operator but it happens to be at the very bottom of the precedence priority list , importantly below the humble comma. So this innocent looking piece of code:

foobar( $a xor $b, $c );

Is interpreted as:

foobar( $a xor ($b, $c) );

And note unlike the not, and and or low-precedence logical operators,  there is no high-precedence xor operator.

So it’s a good idea to wrap xors in parentheses to guarantee intended behaviour:

foobar( ($a xor $b), $c );