http://www.perlmonks.org?node_id=568526


in reply to Glob strange behavior

It's a side effect from calling glob in scalar context:

glob EXPR glob In list context, returns a (possibly empty) list of fil +ename expansions on the value of EXPR such as the standard Un +ix shell /bin/csh would do. In scalar context, glob iterates thr +ough such filename expansions, returning undef when the list + is exhausted. This is the internal function implementing t +he "<*.c>" operator, but you can use it directly. If EXPR +is omit- ted, $_ is used. The "<*.c>" operator is discussed in +more detail in "I/O Operators" in perlop.

Try instead:

... my ($filename)=glob("$path*$_*.pkl"); if (defined($filename)) ...

Update: See graff's explanation for more specifics