Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by LanX (Saint)
on Dec 05, 2012 at 23:53 UTC ( [id://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 (Abbot) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1007430]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-19 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found