http://qs1969.pair.com?node_id=1007424


in reply to Regular expression to list all files of type in folder

Is there perhaps a file called "0" in the directory? That would make the loop prematurely break off.
  • Comment on Re: Regular expression to list all files of type in folder

Replies are listed 'Best First'.
Re^2: Regular expression to list all files of type in folder
by LanX (Saint) on Dec 05, 2012 at 23:53 UTC
    > Is there perhaps a file called "0" in the directory? That would make the loop prematurely break off.

    Thankfully this doesn't happen!!!

    (I was ready to complain about rotten implementations...=)

    Probably saved by one of these special magic behaviors of while

    UPDATE:here it is

    from perlop#I/O-Operators

    The following lines are equivalent:
    while (defined($_ = <STDIN>)) { print; } while ($_ = <STDIN>) { print; } while (<STDIN>) { print; } for (;<STDIN>;) { print; } print while defined($_ = <STDIN>); print while ($_ = <STDIN>); print while <STDIN>;

    This also behaves similarly, but avoids $_ :

    while (my $line = <STDIN>) { print $line }

    In these loop constructs, the assigned value (whether assignment is automatic or explicit) is then tested to see whether it is defined. The defined test avoids problems where line has a string value that would be treated as false by Perl, for example a "" or a "0" with no trailing newline. If you really mean for such values to terminate the loop, they should be tested for explicitly:

    doesn't mention readdir but I think it's a similar case (and a documentation hole)

    Cheers Rolf

      B::Deparse thinks so :)
      $ perl -MO=Deparse,-p -le " opendir BLAH,'.'; while(readdir BLAH){prin +t;}" BEGIN { $/ = "\n"; $\ = "\n"; } opendir(BLAH, '.'); while (defined(($_ = readdir(BLAH)))) { print($_); } -e syntax OK

      Mmh, i tried to reproduce it exactly as described by Phinix and i get the files. Even when i add other files i get them. What do i miss?

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        What do i miss?
        Perl version?

        I'm sure the DWIM magic as demonstrated in this thread was added in some version of Perl, so an older perl can still exhibit the problem.