Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Directory Listing to an Array

by vkonovalov (Monk)
on Apr 29, 2002 at 06:12 UTC ( [id://162776]=note: print w/replies, xml ) Need Help??


in reply to Directory Listing to an Array

This is *dangerous* code snippet, because it corrupts value of $_ variable by while($_=readdir).
(didn't you read "seven sins of perl (revisited)?")
You should place local $_ somewhere at the beginning of a sub. Or write instead of "while" ..... for (readdir) which will localize $_ itself.

Replies are listed 'Best First'.
Re: Re: Directory Listing to an Array
by Anonymous Monk on Feb 28, 2003 at 02:37 UTC

    $map = 'c:\\some\\directory'

    opendir (Directory, $map) or die "Cannot Open Directory";

    @contents = readdir(Directory);

    closedir(Directory);

    splice(@contents, 0, 2);  # removes " . & .. "

    foreach $content (@contents) {

        print "$content\n";

    }

    I have used this to capture the contents of a directory... Darrick...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://162776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found