that one guy has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm a Perl newbie, which should become obvious from the question I am about to ask.

I'm running Perl from the command line on a windows box (Active Perl 5.8.8).

Ultimately, I want to write a program that will search all of the files in a directory for a RegEx and write the results back to a file. It sounds simple enough, but I need to learn to walk first. For starters I'm trying to write a script that will allow me to input the directory name and it will return a list of files in that directory.

My script almost works. Can some one, please, help me? My script is posted below. I think my problem is with my use of <STDIN>.

thanks,
that one guy

#!/usr/bin/perl -w sub namedir { print "Enter the directory path you want to search: "; $dir = <STDIN>; } sub dirme { if (! $dir){ &namedir; } print "\n\nFiles in $dir\n"; opendir(BIN, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir BIN) ) { # list files print "$file\n"; } closedir(BIN); } &dirme;

2006-04-06 Retitled by g0n, as per Monastery guidelines
Original title: '...a little help, please'

Replies are listed 'Best First'.
Re: How to read from STDIN?
by japhy (Canon) on Apr 05, 2006 at 19:20 UTC
    Aside from your lack of 'strict' and 'warnings', and out-dated function-calling style, the real problem is that input from STDIN has a newline at the end of it, and you need to remove it. chomp($dir = <STDIN>)

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to read from STDIN?
by davidrw (Prior) on Apr 05, 2006 at 21:08 UTC
    good excerise on it's own, but also note that File::Find::Rule is a use-existing-code approach:
    my @all_files = File::Find::Rule->file()->maxdepth(1)->in($dir); # or my @matching_files = File::Find::Rule->grep( qr/foo/ )->file()->maxdep +th(1)->in($dir);
Re: How to read from STDIN?
by roboticus (Chancellor) on Apr 06, 2006 at 02:20 UTC
    You may also want to do is look at the '-X' section of the perlfunc man page. These handy tests will help you tell the difference between files and directories & such. As an example, insert the following bit immediately after your # list files comment:

    print "DIR: " if -d $file; print "FILE: " if -f $file;
    --roboticus