I don't understand what glob does here

Discussion in 'Perl Misc' started by Darren Dunham, Aug 6, 2005.

  1. I don't understand the behavior of the following...

    $ touch "file"
    $ perl -e 'foreach $num (0 .. 4) { print "$num .. "; print (scalar glob("file")); print " "; print (scalar glob("file")); { print " ok\n"; } }'
    0 .. file file ok
    1 .. ok
    2 .. file file ok
    3 .. ok
    4 .. file file ok

    I thought that if I were to get "undef" returned on the second scalar
    invocation, I would have seen one "file" in each iteration, not 2
    successful followed by 2 unsuccessful.

    Why does the behavior change between iterations of the foreach loop
    here? I've read through glob and File::Glob and I haven't seen anything
    that appears to explain this.

    Thanks.

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Dunham, Aug 6, 2005
    #1
    1. Advertising

  2. Darren Dunham

    Paul Lalli Guest

    Darren Dunham wrote:
    > I don't understand the behavior of the following...
    >
    > $ touch "file"
    > $ perl -e 'foreach $num (0 .. 4) { print "$num .. "; print (scalar glob("file")); print " "; print (scalar glob("file")); { print " ok\n"; } }'
    > 0 .. file file ok
    > 1 .. ok
    > 2 .. file file ok
    > 3 .. ok
    > 4 .. file file ok
    >
    > I thought that if I were to get "undef" returned on the second scalar
    > invocation, I would have seen one "file" in each iteration, not 2
    > successful followed by 2 unsuccessful.
    >
    > Why does the behavior change between iterations of the foreach loop
    > here? I've read through glob and File::Glob and I haven't seen anything
    > that appears to explain this.


    Each individual glob() call maintains its own state. The first time
    through the for loop is the first call to glob("file") for each glob()
    statement. The second time through the loop is the second call to each
    glob().

    Each glob() only returns the "next" value (undef in the case of only
    one match) for successive calls to the *same* glob() statement,
    regardless of whether or not a different glob() statement has been
    called with the same argument.

    $ perl -le 'print (scalar glob("file")); print (scalar glob("file
    "));print (scalar glob("file"));print (scalar glob("file"));'
    file
    file
    file
    file
    $

    $ perl -le 'print (scalar glob("file")) for 0..3'
    file

    file

    $

    Hope this helps,
    Paul Lalli
    Paul Lalli, Aug 6, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Georgy Pruss
    Replies:
    15
    Views:
    712
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    348
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    340
    David M. Cooke
    May 4, 2004
  4. Elbert Lev

    glob.glob unicode bug or feature

    Elbert Lev, Jul 31, 2004, in forum: Python
    Replies:
    5
    Views:
    382
    Neil Hodgson
    Aug 2, 2004
  5. Hitesh

    glob.glob output

    Hitesh, Mar 12, 2007, in forum: Python
    Replies:
    6
    Views:
    384
    Hitesh
    Mar 13, 2007
Loading...

Share This Page