On 16/12/2010 13:57, practicalperl wrote:
On Thu, Dec 16, 2010 at 9:47 PM, C.DeRykus<dery...@gmail.com> wrote:
There weren't any matches with files and no wildcards
in the glob pattern so the pattern itself is returned *. If
there had been a wildcard in the pattern, then nothing
would have been returned.
Thanks. But still being confused about the case below.
[an...@localhost tmp]$ ls foo*
foo.1 foo.2 foo.3
[an...@localhost tmp]$ cat t2.pl
#!/usr/bin/perl -l
use strict;
for (1..10) {
if (my $x=glob "foo.$_") {
print $x;
print "yes";
} else {
print $x;
print "no";
}
print "-------------";
}
[an...@localhost tmp]$ perl t2.pl
foo.1
yes
-------------
no
-------------
foo.3
yes
-------------
no
-------------
foo.5
yes
-------------
no
-------------
foo.7
yes
-------------
no
-------------
foo.9
yes
-------------
no
-------------
From "perldoc -f glob":
In scalar context, glob iterates through ... filename expansions,
returning undef when the list is exhausted.
So glob() is returning the file pattern itself on the first call, as
Charles described, and then undef on the second call to indicate that
the list is complete. The cycle continues on the third call and so on.
To avoid this effect, call glob() in list context, like this perhaps:
for (1..10) {
if (my @x = glob "foo.$_") {
print "@x\n";
print "yes";
}
else {
print "@x\n";
print "no";
}
print "-------------";
}
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/