[RndTbl] strange race condition on file entries

Trevor Cordes trevor at tecnopolis.ca
Sun Feb 2 15:10:46 CST 2014


I forgot to post what *does* work.  There are a few different
ways.

You can force the glob to return a list (hence no caching/iterating):

if (!@{[<$prefix-*>]}) {

That's sure ugly, but it makes sense.  [] forces the <> to return list
and turns it into an anon array-ref.  @ then derefs.  {} is required by
the parser to make sense of it all.  !@ then does its normal thing of
meaning "if there are zero elements in this array".

You could also force it to list by assigning to an array:

if (!(@a=<$prefix-*>)) {

But I hate using temp vars when we don't need to; when the results are
throwaway.

Strangely, using the normal "we want a list" syntax of just putting ()
around the expression doesn't work in this case.  I'm not sure why:

if (!(<$prefix-*>)) {  # doesn't work

Also strangely, while perl has a "scalar" function to force a scalar
(non-list) result, it doesn't have a "list" or "array" function to
force a list.  The scalar docs say list isn't necessary as it's usually
the default/implied or can be forced with ().  Woops, looks like I
found a corner case.

Ya, I could have been certain everything worked in the first place by
doing opendir and iterating through the results, like one would in
C, but the whole purpose of using perl is to do as much as possible in
as few characters and lines.  Laziness is the #1 programmer virtue,
according to L.Wall and R.Schwarz.


More information about the Roundtable mailing list