[Za-pm] getting out of <STDIN> loop

Anne Wainwright anotheranne at fables.co.za
Sun Aug 9 05:39:32 PDT 2009


Hi, all,

(Am in fact referring to Exercise 2 Chapter 2 of 
Schwartz et al INTERMEDIATE PERL.)

First, while realising that the official solution is a masterpiece of
condensed perl code, I don't understand why the first
line has the (1) ).

-----------------------------------------------------------
while(1) { 
    print "Enter a regular expression to match filenames> ";
    chomp(my $regex = <STDIN>);
    last unless (defined $regex && length $regex);
    print map {"    $_\n"} grep {eval{/$regex/}} glob(".* *");
}
-----------------------------------------------------------

My own more primitive solution fails to get out of the
input loop, albeit with simpler criteria. My code is:

-------------------------------------------------------
opendir THISDIR, "." or die "serious braindamage: $!";
@allfiles = readdir THISDIR;
closedir THISDIR;

while (defined($regexpr = <STDIN>)) {		# 1    
        chomp $regexpr;  
        foreach $file(@allfiles) {        	# 2
	        if ($file =~ m/$regexpr/) {     # 3
		    print "$file\n";
	        }                               # -3
        }                                   	# -2
}						# -1
-------------------------------------------------------
Essentially on no regex input it outputs all the files as if ".+" had
been input.

I thought the line marked #1 was a dead ringer for getting out of the
input loop. (p.72 Chapter 6 of Schwartz et al LEARNING PERL). If I take
"&& length $regex" out of their code then that also fails to exit. I
don't understand  :(

Any one explaining this in small understandable words thanked
in advance.

bestest
Anne


More information about the Za-pm mailing list