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.

3 Comments

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.*")).

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.