Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Regular expression to list all files of type in folder

by LanX (Chancellor)
on Dec 05, 2012 at 23:53 UTC ( #1007430=note: print w/replies, xml ) Need Help??


in reply to Re: Regular expression to list all files of type in folder
in thread 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.

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

Replies are listed 'Best First'.
Re^3: Regular expression to list all files of type in folder
by Anonymous Monk on Dec 06, 2012 at 14:00 UTC
    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
Re^3: Regular expression to list all files of type in folder
by karlgoethebier (Prior) on Dec 06, 2012 at 17:09 UTC

    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.

        OK, thank you!

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007430]
help
Chatterbox?
[Corion]: I should write some more code for my Jekyll clone, but I don't feel like it, so maybe I should just let that linger instead ;)
[karlgoethebier]: "...mein enemy..."
[marto]: it has finally stopped raining!
[Corion]: marto: Heh - we've got the rain here, now. But it's only intermittently, not permanent. But funny that you should complain about rain ;)
[Discipulus]: rain? i dont remember it..
[marto]: and good news all round, Flash will die come 2020!
[Corion]: So who will be the saviour of the universe, then?!
[marto]: Corion++, asking the right questions

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-07-26 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (390 votes). Check out past polls.