Home > Archive > PERL Miscellaneous > March 2006 > file glob in scalar context
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
file glob in scalar context
|
|
| nan li 2006-03-29, 7:00 pm |
| Hello,
I have a question about glob in scalar context.
The perldoc says,
' In scalar context, glob iterates through such
filename expansions, returning undef when
the list is exhausted.'
So I wrote the following and it returns all pl files.
while ( my $f = glob "*.pl" ) {
print $f, "\n";
}
But without a loop, glob seems to give me just the first
file all the time.
my $f = glob "*.pl";
print $f, "\n"; # "first.pl"
$f = glob "*.pl";
print $f, "\n"; # "first.pl" again
Does any one have an explanation. I am .
Thanks,
Nan
| |
| xhoster@gmail.com 2006-03-29, 7:00 pm |
| "nan li" <nan.li.g@gmail.com> wrote:
> Hello,
>
> I have a question about glob in scalar context.
>
> The perldoc says,
> ' In scalar context, glob iterates through such
> filename expansions, returning undef when
> the list is exhausted.'
>
> So I wrote the following and it returns all pl files.
>
> while ( my $f = glob "*.pl" ) {
> print $f, "\n";
> }
>
> But without a loop, glob seems to give me just the first
> file all the time.
>
> my $f = glob "*.pl";
> print $f, "\n"; # "first.pl"
>
> $f = glob "*.pl";
> print $f, "\n"; # "first.pl" again
>
> Does any one have an explanation. I am .
You have two separate globs. Each maintains separate, independent,
state.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Kraven 2006-03-29, 7:00 pm |
| nan li wrote:
> Hello,
>
> I have a question about glob in scalar context.
>
> The perldoc says,
> ' In scalar context, glob iterates through such
> filename expansions, returning undef when
> the list is exhausted.'
>
> So I wrote the following and it returns all pl files.
#get a file name with an extension called .pl
> while ( my $f = glob "*.pl" ) {
#print that file
> print $f, "\n";
#don't exit the program yet we haven't seen undef!! Do it all again...
> }
>
> But without a loop, glob seems to give me just the first
> file all the time.
>
>
#let's make $f equal the file name that glob returns
> my $f = glob "*.pl";
#print that file name
> print $f, "\n"; # "first.pl"
#looks like we're done here, exit the program
>
>
#same as your second example
> $f = glob "*.pl";
> print $f, "\n"; # "first.pl" again
>
> Does any one have an explanation. I am .
>
> Thanks,
> Nan
>
| |
| Kraven 2006-03-29, 7:00 pm |
| Kraven wrote:[color=darkred]
> nan li wrote:
>
>
>
> #get a file name with an extension called .pl
>
>
> #print that file
>
>
> #don't exit the program yet we haven't seen undef!! Do it all again...
>
> #let's make $f equal the file name that glob returns
>
>
> #print that file name
>
>
> #looks like we're done here, exit the program
>
> #same as your second example
>
ps... maybe you want something like this:
use strict;
use warnings;
my @offiles = glob "*.pl";
foreach (@offiles) {
print "$_\n";
}
| |
| nan li 2006-03-29, 7:00 pm |
|
xhoster@gmail.com wrote:
> "nan li" <nan.li.g@gmail.com> wrote:
>
> You have two separate globs. Each maintains separate, independent,
> state.
>
> Xho
Thanks. It seems in a loop, the glob is executed only once, and returns
something
like a stream handle. I'd really like to know how it is implemented.
But anyway,
I would say it is very counterintuitive because the following 2 are
not the same.
___________________________
for ( 1 .. 2 ) {
$f = glob "*.pl";
print $f, "\n";
}
___________________________
$f = glob "*.pl";
print $f, "\n";
$f = glob "*.pl";
print $f, "\n";
__________________________
| |
| George 2006-03-29, 7:00 pm |
| > > nan li wrote:
Kraven wrote:[color=darkred]
> ps... maybe you want something like this:
>
> use strict;
> use warnings;
>
> my @offiles = glob "*.pl";
> foreach (@offiles) {
> print "$_\n";
> }
....or even just use a simple wrapper subroutine, like 'glob1' for
example:
use strict;
use warnings;
sub glob1 { glob shift }
my $f;
$f = glob1 "*.pl"; print $f, "\n"; # "first.pl"
$f = glob1 "*.pl"; print $f, "\n"; # "second.pl"
| |
| xhoster@gmail.com 2006-03-29, 7:00 pm |
| "nan li" <nan.li.g@gmail.com> wrote:
> xhoster@gmail.com wrote:
....[color=darkred]
>
> Thanks. It seems in a loop, the glob is executed only once, and returns
> something like a stream handle.
Kind of like that. It doesn't return the handle, it just stores it
internally and serves files out of it.
> I'd really like to know how it is implemented.
Did you every hear the joke of the two economists who see a sports car
drive by? One of them says "I always wanted a car like that." The other
one says "Obviously not."
Perl is open source. If you *really* would like to know how it is
implemented, you can go look and see. :)
> But anyway,
> I would say it is very counterintuitive because the following 2 are
> not the same.
>
> ___________________________
> for ( 1 .. 2 ) {
> $f = glob "*.pl";
> print $f, "\n";
> }
> ___________________________
>
> $f = glob "*.pl";
> print $f, "\n";
> $f = glob "*.pl";
> print $f, "\n";
Your intuition can be trained. To me, it is intuitive because I
intuit it like the /g on a scalar-context regex:
$ perl -lw
for (1..2) {
"foobar" =~ /(...)/g;
print $1;
};
__END__
foo
bar
$ perl -lw
"foobar" =~ /(...)/g;
print $1;
"foobar" =~ /(...)/g;
print $1;
__END__
foo
foo
But I pretty much never use glob in a scalar context, anyway.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
|
|
|
|
|