Firstly, arrays cannot be undefined. You check if the array is empty. To understand why it is empty, you need to understand -n . -n surrounds your code with
LINE: while (<>) { ... }
which is not enough for
LINE: while (defined($_ = <ARGV>)) { ... }
ARGV is a magic descriptor that reads files listed in @ARGV , rearranging file names when they open them.
$ echo foo1 > foo $ echo foo2 >>foo $ echo bar1 > bar $ echo bar2 >>bar $ echo baz1 > baz $ echo baz2 >>baz $ perl -nlE' BEGIN { say "Files to read: @ARGV" } say "Read $_ from $ARGV. Files left to read: @ARGV"; ' foo bar baz Files to read: foo bar baz Read foo1 from foo. Files left to read: bar baz Read foo2 from foo. Files left to read: bar baz Read bar1 from bar. Files left to read: baz Read bar2 from bar. Files left to read: baz Read baz1 from baz. Files left to read: Read baz2 from baz. Files left to read:
Keep in mind that BEGIN blocks are executed as soon as they are compiled, therefore <ARGV> is not executed yet when the BEGIN block is executed (even if it appears earlier in the program), therefore @ARGV has not yet been changed.
-n documented in perlrun . ARGV , @ARGV and $ARGV described in perlvar .
source share