|
Posted by Eric Pozharski on June 24, 2008, 3:42 pm
Please log in for more thread options
*SKIP*
>> As of Perl 5.8.8 (for Debian Etch) it makes no difference:
> You're testing the wrong thing... which is not surprising, since where
> Perl keeps its implicit iterators is never very clear.
That's the second time I see you suppose some magic behind interpreter.
I like it. I suppose too.
> Scalar glob keeps its iterator *in the op*: that is, each instance of
[ I'm sorry about that thread. It was based on my sole misunderstanding
what are (differences among) lists, arrays, assignments in scalar and
list context; and incomplete process of familarizing myself with
precedence and associativity. Now I feel much beter. ]
*SKIP*
>> 10:24:07 34 [0:0]$ perl -e 'print scalar(@{[ glob q(/*) ]}) > 1; print
>> "\n"'
> This works correctly, but again is deeply ugly.
> The idiom you are looking for is
> my $x = () = glob(...);
> or, if you need a 'scalar',
> print scalar( () = glob(...) );
That's what I missed (yes, I admit missing I<-l> and B<I<-w>>). To put
it clear:
22:16:07 143 [0:0]$ perl -le '$x = (1,2,4); print $x'
4
22:16:14 144 [0:0]$ perl -le '($x) = (1,2,4); print $x'
1
22:16:18 145 [0:0]$ perl -le '$x = () = (1,2,4); print $x'
3
The 1st is a B<comma operator>, I know. The 3rd slowly moves down to
backbone where it will stay forever. What surprises me most is the 2nd.
A long time ago, when I've just started the addiction to Perl, I was
told (OK, I've read), that intentional way to do that is
C<($x, undef) = (1, 2, 3)>; either way (C<($x) = (1, 2, 3)>) will set
I<$x> to the number of items in list. It's not so. Any more?
I've just checked, replacing the list with explicit array merges 1st and
3rd cases. And has no influence on the 2nd. Obviously, I've missed
some reading. What?
> I have no idea where this is documented, but if you assign to the empty
> list in scalar context it returns a count of the items in the assigned
> list. This is weird and not at all the way any other list assignment
> works, but surprisingly useful.
L<perlop/"Assignment operators">. In the last paragraph it's mentioned;
no explicit example though.
--
Torvalds' goal for Linux is very simple: World Domination
|