Bleg: Why the alternating result?
I know I made a mistake by doing a double glob, but why this pattern of output? Note, there is only one file matching /etc/magic.* and two files matching /etc/magic*, and none matching /etc/magic.dragon*
% perl -E'say (glob </etc/magic.*>) for 1..10'
/etc/magic.mime
2
/etc/magic.mime
4
/etc/magic.mime
6
/etc/magic.mime
8
/etc/magic.mime
10
% perl -E'say (glob </etc/magic*>) for 1..10'
/etc/magic
/etc/magic.mime
3
/etc/magic
/etc/magic.mime
6
/etc/magic
/etc/magic.mime
9
/etc/magic
% perl -E'say (glob </etc/magic.dragon*>) for 1..10'
I'm really hating Perl's context today.
From `perldoc -f glob`:
In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
From `perldoc -f say`:
Just like "print", but implicitly appends a newline.
From `perldoc -f print`:
If LIST is also omitted, prints $_ to the currently selected output channel.
So, `say` prints each file, one per line until `glob` returns a undef, then `say` prints the contents of $_, which is a number. Then `glob` restarts.
You're mixing up your globbing syntax. You use either <foo.*> or glob("foo.*") but not both. You're effectively calling glob(glob("foo.*")).
@Andy: Yup, I'm aware of that. What I was wondering is why the output is like it is.
@shawnhcorey: Thanks. I didn't know glob() behaves differently in scalar context. And didn't know that the inner glob() is evaluated in scalar context (due to the outer glob() expecting only a single argument).
Anybody else bitten by this? :-)