Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: list dir contents, w/o some stuff

by Rich36 (Chaplain)
on Jan 14, 2002 at 12:21 UTC ( [id://138541]=note: print w/replies, xml ) Need Help??


in reply to list dir contents, w/o some stuff

You're very close on this one...
One thing you will see a lot around here - use strict and the -w flag.
One of the things that using the warning flag and the strict pragma would have caught is that you have two different names for the array that you use to contain the files you don't want to list - @nowshow and @noshow. Using strict and the -w flag can save a lot of heartache and headaches in finding problems in your code. Also, if this is a CGI script, you want to look into the -T flag. Take a look at perlsec for more information.

The other problem is with the unless statement. $f eq @noshow won't work. It won't tell you if $f is equal to a value in @noshow. What you need to do is use the grep function. This pulls out information from a list based on the criteria that you set. In the code below, it searches for $f in the list of filenames not to display. If it finds it, it won't print it out.

Finally, another recommendation - use CGI or die. The module CGI.pm provides many, many built in functions for creating CGI scripts. It's invaluable for creating CGI programs.

#!/usr/bin/perl -w print "Content-type: text/html\n\n"; opendir(DIR,"./") || die print "Couldn't open directory"; my @files = readdir(DIR); closedir(DIR); my $filename = __FILE__; my @noshow = (".", "..", $filename); foreach $f (@files) { unless (grep /$f/, @noshow) # look for $f in @noshow { print "$f <br>\n"; } }

Rich36
There's more than one way to screw it up...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found