On 3/23/06, Christopher Spears <[EMAIL PROTECTED]> wrote: > I've been reading the Intermediate Perl book and am > trying to solve one of the exercises. I wrote a > script that takes input from the keyboard and uses the > input as a regular expression to search for files in a > directory. If the script finds a match, the filename > is printed out. > > #!/usr/bin/perl -w > use strict; > > print "Enter a regular expression: "; > chomp(my $pattern = <STDIN>); > > my $some_dir = "./ex2"; > opendir(DIR, $some_dir) || die "Can't open $some_dir: > $!"; > my @filenames = readdir(DIR); > > foreach (@filenames) { > if (eval {$_ =~ /$pattern/} ) { > print $_ . "\n"; > } > print "Continuing after error: $@" if $@; > } > > I want the program to keep asking the user for a > pattern until an empty string is entered. I > remembered how to do this once, but I am returning to > Perl after learning another language. I need to jog > my memory! >
<> will do what you're looking for, or <STDIN> if you prefer.: my $some_dir = "./ex2"; opendir(DIR, $some_dir) || die "Can't open $some_dir: $!\n"; my @filenames = readdir(DIR); while ( chomp(my $pattern = <>) ) { last if $pattern =~ /^\s*$/; foreach (@filenames) { if (eval {$_ =~ /$pattern/} ) { print $_ . "\n"; } print "Continuing after error: $@" if $@; } } If you're willing to just go with the standard EOF (ctrl-d on most platforms), you don't even need to test the return value. STDIN/<> close automatically when they encounter EOF and the loop will exit. -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!