Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Glob strange behavior

by johnnywang (Priest)
on Aug 21, 2006 at 23:37 UTC ( #568719=note: print w/replies, xml ) Need Help??


in reply to Glob strange behavior

Can someone actually explain this? why glob in scalar context will return undef for every other file? The documentation says in scalar context glob returns an iterator(which is ok, so one can do a while loop to go through all of them). But why does it fail for every other file? I thought it might be some side effects on $_, but that's not. Thanks.

Replies are listed 'Best First'.
Re^2: Glob strange behavior
by graff (Chancellor) on Aug 22, 2006 at 02:24 UTC
    The explanation was alluded to above, but not actually spelled out in the thread. It lies in the "I/O Operators" section of the perlop man page:
    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 "undef" when the list has 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.

    That's some pretty arcane stuff, but I think it gets the point across, which is: even though you would expect the file glob to "start fresh" each time you use it with a new string, it won't, unless it has already reached the end of a list that it built on a previous call.

    As used in the OP (globbed string matches one file, glob is used in a scalar context), it takes two iterations to reach the end of the first list, and thereafter only the odd-numbered glob strings are actually processed.

      Hi fellows, i would like to point out the different behavior of glob utilized in scalar context when the key is a variable or a literal.
      WRONG my @sign_arr=('A1' .. 'A9'); for my $sign (@sign_arr) { my $filename = <*$sign*.pkl>; print "$filename\n" if defined($filename); } # OUTPUT: # file_A1_.pkl # file_A3_.pkl # file_A5_.pkl # file_A7_.pkl # file_A9_.pkl # RIGHT my $filename; $filename = <*A1*.pkl>; print "$filename\n" if defined($filename); $filename = <*A2*.pkl>; print "$filename\n" if defined($filename); $filename = <*A3*.pkl>; print "$filename\n" if defined($filename); $filename = <*A4*.pkl>; print "$filename\n" if defined($filename); $filename = <*A5*.pkl>; print "$filename\n" if defined($filename); $filename = <*A6*.pkl>; print "$filename\n" if defined($filename); $filename = <*A7*.pkl>; print "$filename\n" if defined($filename); $filename = <*A8*.pkl>; print "$filename\n" if defined($filename); $filename = <*A9*.pkl>; print "$filename\n" if defined($filename); # OUTPUT: # file_A1_.pkl # file_A2_.pkl # file_A3_.pkl # file_A4_.pkl # file_A5_.pkl # file_A6_.pkl # file_A7_.pkl # file_A8_.pkl # file_A9_.pkl
      A bug?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://568719]
help
Chatterbox?
[Corion]: Do you want to launch a script and keep the command prompt/console window open?
[Corion]: Do you want to wait for a key press before closing the window?
[LanX]: I want the command line in the history
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.
[LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy
[Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history
LanX damns the cult of CB ;-)
LanX WTF WTF WTF
[LanX]: please forget my last 3 posts

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-03-27 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.