Hi Anne<br><br>2 reasons why your while loop does not terminate on empty input:<br><br>First, the &#39;length&#39; issue: Before chomping, length of regexpr when you
just press Enter is 1 (Still has the newline character) , after 
chomping it is 0<br>
  So you need to check after chomping or say <br><br>     ....<br>     while (length($regexpr = &lt;STDIN&gt;)&gt;1) {           # 1<br>     ....<br>
<br>Secondly, after chomping, the empty string is still a defined value:<br><br>perl -e &#39;print &quot;defined&quot; if defined &quot;&quot; &#39;<br><br>The reason why presumably the defined test is part of the loop, is that it if you enter<br>
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  <br>
<br>Ctrl-C is a *nix shell control character which terminates the command, so does not get to be processed in the while loop.<br>
<br>The reason all the files are printed out with an empty string is that it matches all the filenames<br><br>perl -e &#39;print &quot;match&quot; if &quot;anyfilenameyoulike&quot;=~//&#39;<br>perl -e &#39;print &quot;match&quot; if &quot;&quot;=~//&#39;<br>

<br>Regards<br><br><div class="gmail_quote">On Sun, Aug 9, 2009 at 2:39 PM, Anne Wainwright <span dir="ltr">&lt;<a href="mailto:anotheranne@fables.co.za">anotheranne@fables.co.za</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi, all,<br>
<br>
(Am in fact referring to Exercise 2 Chapter 2 of<br>
Schwartz et al INTERMEDIATE PERL.)<br>
<br>
First, while realising that the official solution is a masterpiece of<br>
condensed perl code, I don&#39;t understand why the first<br>
line has the (1) ).<br>
<br>
-----------------------------------------------------------<br>
while(1) {<br>
    print &quot;Enter a regular expression to match filenames&gt; &quot;;<br>
    chomp(my $regex = &lt;STDIN&gt;);<br>
    last unless (defined $regex &amp;&amp;  length $regex);<br>
    print map {&quot;    $_\n&quot;} grep {eval{/$regex/}} glob(&quot;.* *&quot;);<br>
}<br>
-----------------------------------------------------------<br>
<br>
My own more primitive solution fails to get out of the<br>
input loop, albeit with simpler criteria. My code is:<br>
<br>
-------------------------------------------------------<br>
opendir THISDIR, &quot;.&quot; or die &quot;serious braindamage: $!&quot;;<br>
@allfiles = readdir THISDIR;<br>
closedir THISDIR;<br>
<br>
while (defined($regexpr = &lt;STDIN&gt;)) {           # 1<br>
        chomp $regexpr;<br>
        foreach $file(@allfiles) {              # 2<br>
                if ($file =~ m/$regexpr/) {     # 3<br>
                    print &quot;$file\n&quot;;<br>
                }                               # -3<br>
        }                                       # -2<br>
}                                               # -1<br>
-------------------------------------------------------<br>
Essentially on no regex input it outputs all the files as if &quot;.+&quot; had<br>
been input.<br>
<br>
I thought the line marked #1 was a dead ringer for getting out of the<br>
input loop. (p.72 Chapter 6 of Schwartz et al LEARNING PERL). If I take<br>
&quot;&amp;&amp; length $regex&quot; out of their code then that also fails to exit. I<br>
don&#39;t understand  :(<br>
<br>
Any one explaining this in small understandable words thanked<br>
in advance.<br>
<br>
bestest<br>
Anne<br>
_______________________________________________<br>
Za-pm mailing list<br>
<a href="mailto:Za-pm@pm.org">Za-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/za-pm" target="_blank">http://mail.pm.org/mailman/listinfo/za-pm</a><br>
</blockquote></div><br><br clear="all"><br>