Why does glob() work in while loop?

Discussion in 'Perl Misc' started by J. Romano, Jan 27, 2005.

  1. J. Romano

    J. Romano Guest

    Dear Perl community,

    I was recently explaining to a friend new to Perl about while loop
    conditions and how they implictly assign a value to $_ only if the
    condition looks like:

    while (<HANDLE>)

    and not like:

    while (function())

    To demonstrate an incorrect example, I executed this following code
    that contains glob() in the while condition:


    #!/usr/bin/perl
    use strict;
    use warnings;

    while (glob("*"))
    {
    print "$_\n";
    }
    __END__


    I was surprised to see that the program worked as expected! (That
    is, it listed every file in my directory.) I didn't expect it to do
    that, because I didn't think that the while condition would populate
    $_ for me.

    I played around a little more, and came up with a new script:


    #!/usr/bin/perl
    use strict;
    use warnings;

    {
    my $num = 0;

    sub getNextNum
    {
    return ++$num;
    }
    }

    while (getNextNum())
    {
    print "$_\n";
    sleep(1);
    }
    __END__


    Instead of calling the glob() function in the while condition, the
    getNextNum() function is called, which just returns an integer (the
    first time it is called, it returns 1; every other time, it returns a
    number one greater than the previous call).

    Now, when I run this program, those new to Perl might expect that
    the return value gets put in $_ and then gets printed out. Instead, I
    get:

    Use of uninitialized value in concatenation (.) or string at ... line
    16.

    which makes sense to me, because I know that the return value of
    getNextNum() is never placed in $_.

    But if the return value of getNextNum() is not placed in $_, then
    doesn't that mean that the return value of glob() also should not be
    placed in $_? If my reasoning is correct, why does the first program
    I gave populate $_?

    Thanks in advance for any explanations.

    -- Jean-Luc
    J. Romano, Jan 27, 2005
    #1
    1. Advertising

  2. J. Romano

    Jay Tilton Guest

    (J. Romano) wrote:

    : #!/usr/bin/perl
    : use strict;
    : use warnings;
    :
    : while (glob("*"))
    : {
    : print "$_\n";
    : }
    : __END__
    :
    :
    : I was surprised to see that the program worked as expected! (That
    : is, it listed every file in my directory.) I didn't expect it to do
    : that, because I didn't think that the while condition would populate
    : $_ for me.

    [snip]

    : But if the return value of getNextNum() is not placed in $_, then
    : doesn't that mean that the return value of glob() also should not be
    : placed in $_? If my reasoning is correct, why does the first program
    : I gave populate $_?

    This is explained in the documentation for the glob function. It's got a
    splash of DWIM built in.
    Jay Tilton, Jan 27, 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:
    716
    Tim Roberts
    Dec 1, 2003
  2. Tim Peters
    Replies:
    1
    Views:
    350
    Duncan Booth
    Dec 1, 2003
  3. Sean Berry

    Question about glob.glob <--newbie

    Sean Berry, May 4, 2004, in forum: Python
    Replies:
    3
    Views:
    342
    David M. Cooke
    May 4, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page