Subject: | dir_list in Scalar Context |
It's me again. The docs don't define what dir_list does in scalar context, which is to return the number of items that would have been returned in the list in list context.
I'm trying to determine whether a file is in a CVS attic, that is whether its parent dir is called 'Attic'. I first tried this:
if $file->parent->dir_list(-1) eq 'Attic'
That doesn't work, because dir_list returns the integer 1, indicating that there would be one item in the list -- something that I know, since I asked for exactly one item! So I'm having to write it like this:
if ($file->parent->dir_list(-1))[0] eq 'Attic'
And that kind-of defeats one of the purposes of dir_list if the result has to be subscripted again. The above is of course the same as:
if ($file->parent->dir_list)[-1] eq 'Attic'
which gets rid of one of the subscripts, but it seems a waste to have a feature for doing this but not use it!
Personally I'd prefer it if both wantarray tests were removed from dir_list and the list returned in all circumstances; I can't think of a situation where the list length would be useful to me, but grabbing just one element like this is something I'd want. This means that if a user asks for multiple elements in scalar context they only get one of them, but it's quite Perl-ish not to object if a user asks for something stupid.
Since the current behaviour isn't documented, nobody should be relying on it (because if they were, surely they'd've sent you a doc patch ...).
(Alternatively, if you judge that the current behaviour is useful to have then it should be documented so that anybody wanting this can rely on it!)
Best wishes.
Smylers