glob in scalar context:
In a scalar context, glob iterates through such file name extensions, returning undef when the list is exhausted.
IN
foreach (@list_env_vars){ print "$_ = ".glob()."\n"; }
glob() really is glob($_) . Each iteration of $_ contains the string $SERVER . Given that the environment variable does not change, $SERVER expands to the same line. This string is returned for the first time. Then the list is exhausted, so undef returned. The third time we start ....
Explanation: It does not matter that the argument of the second call is the same as for the first call, since there is no way to reset the glob iterator.
This can be done more clearly using the following example (the current directory contains files 1.a ', 1.b', '2.a' and '2.b'):
#!/usr/bin/perl -w use strict; my @patterns = ( '*.a', '*.b', ); for my $v ( @patterns ) { print "$v = ", scalar glob($v), "\n"; }
Output:
C: \ Temp> d
* .a = 1.a
* .b = 2.a
I would recommend accessing the environment variables through the %ENV hash:
my @list_env_vars = ($ENV{SERVER}) x 6;
or
my @list_env_vars = @ENV{qw(HOME TEMP SERVER)};
source share