G'day Marshall,

I think the issue here is that the OP wrote "read a file", not "read from the keyboard". If the sentinel value is detected, then processing of the file should stop; however, I think it's reasonable to assume that if no sentinel value is detected, then processing of the file should stop at the end of the file.

Note that in the examples below, I used a common alias of mine:

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'

Here's a file that simulates your keyboard input. I've added an extra line that is to be ignored:

$ cat > pm_11133571.dat 234 asdf quit ignore

That is processed (almost) identically to your keyboard input:

ken@titan ~/tmp $ perle 'my $line; while ( (print "list of letters: "),$line=<>, $line + !~ /\s*quit|exit|q\s*$/i) { print "doing something with $line\n"}' p +m_11133571.dat list of letters: doing something with 234 list of letters: doing something with asdf list of letters: ken@titan ~/tmp

I said "almost" because, as you may have noticed, there's a dangling "list of letters: " prompt as the last line of output.

However, if there's no sentinel value, processing does not stop at the end of the file; instead, the code is stuck in a loop. Also, warnings are emitted. Both of those things are what ++ikegami predicted.

Consider this slightly different input file (without a sentinel):

$ cat > pm_11133571_2.dat 234 asdf ignore

Here's what happens:

$ perle 'my $line; while ( (print "list of letters: "),$line=<>, $line + !~ /\s*quit|exit|q\s*$/i) { print "doing something with $line\n"}' p +m_11133571_2.dat list of letters: doing something with 234 list of letters: doing something with asdf list of letters: doing something with ignore Use of uninitialized value $line in pattern match (m//) at -e line 1, +<> line 3. at -e line 1, <ARGV> line 3. Use of uninitialized value $line in concatenation (.) or string at -e +line 1, <> line 3. at -e line 1, <ARGV> line 3. list of letters: doing something with list of letters:

The screen output stopped there; I used Ctrl-C to get back to my shell prompt. Of course, I could've typed one of the sentinel values (quit|exit|q) but would you expect users to know what those values were; and even if they did, would you expect them to do this in the "production" environment you described.

Update: I started to compose my response before your post had any updates. After posting, I see you've added three updates. That's not a complaint about the updates; just advice on what I was responding to.

— Ken


In reply to Re^5: $_ not set in while <> by kcott
in thread $_ not set in while <> by pidloop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.