file glob in scalar context

N

nan li

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 confused.

Thanks,
Nan
 
X

xhoster

nan li said:
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 confused.

You have two separate globs. Each maintains separate, independent,
state.

Xho
 
K

Kraven

nan said:
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
 
K

Kraven

Kraven said:
#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";
}
 
N

nan li

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";
__________________________
 
X

xhoster

nan li said:
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
 
G

George

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"
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top