Perl 5.8.0 foreach & glob BUG

C

Craig Manley

Hi all,

Please check out the code below that shows that when I iterate of an array
of values for use in a glob() function, then glob() returns an empty string
for every odd array index. This is really weird. I don't see how 'foreach'
and 'glob' can be connected here in any way. In all my years of Perl
programming, I've never come across a single Perl bug, so even though this
really does seem like a bug, perhaps I'm overlooking something. Perl version
is 5.8.0.

-bash-2.05b$ perl
use strict;
foreach my $value (1,2,3,4,5,6,7) {
my $filename = glob("~/var/util/import/cdr/$value.csv");
print "Value: $value\tFilename: $filename\n";
}
#### ctrl - D pressed here ###
Value: 1 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/1.csv
Value: 2 Filename:
Value: 3 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/3.csv
Value: 4 Filename:
Value: 5 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/5.csv
Value: 6 Filename:
Value: 7 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/7.csv
-bash-2.05b$

-Craig Manley
 
J

John W. Krahn

Craig said:
Please check out the code below that shows that when I iterate of an array
of values for use in a glob() function, then glob() returns an empty string
for every odd array index. This is really weird. I don't see how 'foreach'
and 'glob' can be connected here in any way. In all my years of Perl
programming, I've never come across a single Perl bug, so even though this
really does seem like a bug, perhaps I'm overlooking something. Perl version
is 5.8.0.

-bash-2.05b$ perl
use strict;
foreach my $value (1,2,3,4,5,6,7) {
my $filename = glob("~/var/util/import/cdr/$value.csv");
print "Value: $value\tFilename: $filename\n";
}
#### ctrl - D pressed here ###
Value: 1 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/1.csv
Value: 2 Filename:
Value: 3 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/3.csv
Value: 4 Filename:
Value: 5 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/5.csv
Value: 6 Filename:
Value: 7 Filename:
/var/www/html/vhosts/mijnbel/.//var/util/import/cdr/7.csv
-bash-2.05b$

That is not a bug, it is the defined behaviour for using glob in scalar context.

perldoc perlop
[snip]
A (file)glob evaluates its (embedded) argument only when
it is starting a new list. All values must be read before
it will start over. In list context, this isn't important
because you automatically get them all anyway. However,
in scalar context the operator returns the next value each
time it's called, or C run out. As with filehandle reads,
an automatic `defined' is generated when the glob occurs
in the test part of a `while', because legal glob returns
(e.g. a file called 0) would otherwise terminate the loop.
Again, `undef' is returned only once. So if you're
expecting a single value from a glob, it is much better to
say

($file) = <blurch*>;

than

$file = <blurch*>;

because the latter will alternate between returning a
filename and returning false.



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top