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

Francois Marais francois at busii.com
Tue Aug 11 10:35:58 PDT 2009


Hi Anne

2 reasons why your while loop does not terminate on empty input:

First, the 'length' issue: Before chomping, length of regexpr when you just
press Enter is 1 (Still has the newline character) , after  chomping it is 0
  So you need to check after chomping or say

     ....
     while (length($regexpr = <STDIN>)>1) {           # 1
     ....

Secondly, after chomping, the empty string is still a defined value:

perl -e 'print "defined" if defined "" '

The reason why presumably the defined test is part of the loop, is that it
if you enter
Ctrl-D  (That is  on *nix) and dont check for it, and you use warnings
(which you should be doing), you will get a warning

Ctrl-C is a *nix shell control character which terminates the command, so
does not get to be processed in the while loop.

The reason all the files are printed out with an empty string is that it
matches all the filenames

perl -e 'print "match" if "anyfilenameyoulike"=~//'
perl -e 'print "match" if ""=~//'

Regards

On Sun, Aug 9, 2009 at 2:39 PM, Anne Wainwright <anotheranne at fables.co.za>wrote:

> 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
> _______________________________________________
> Za-pm mailing list
> Za-pm at pm.org
> http://mail.pm.org/mailman/listinfo/za-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/za-pm/attachments/20090811/94c9461e/attachment.html>


More information about the Za-pm mailing list