Ah, welcome aboard, wageslave!

To answer your question about $_, you can read that variable as "it". When you write something like print $_, you are saying "print it". You can, as you've discovered, leave the "it" out, and Perl will know what you mean.

As for the problems you're having in your three code snippets, here's a quote from perldoc -f eof to get us started:

eof FILEHANDLE eof () eof Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open. FILEHANDLE may be an expression whose value gives the real filehandle. (Note that this function actually reads a character and then "ungetc"s it, so isn't very useful in an interactive context.) Do not read from a terminal file (or call "eof(FILEHANDLE)" on it) after end-of-file is reached. File types such as terminals may lose the end-of-file condition if you do.

What I'd like to point out from there is that eof is not a value, but rather a function. So when you do <STDIN>!=eof, you compare <STDIN> with either true or false. That's not something that's very printable, or something that makes a lot of sense. This is also why your second loop doesn't work.

Also, again from perldoc -f eof, I present the following snippet:

Practical hint: you almost never need to use "eof" in Perl, because the input operators typically return "undef" when they run out of data, or if there was an error.

Perl has a very practical view on what is true or false. When you do while (<STDIN>), you will stop when <STDIN> returns undef, which is considered false.

Keeping all of this in mind, here's how I might re-write your loop:

while (<STDIN>) { print; print "***\n"; }

Alternatively, I might write:

while (<STDIN>) { print $_, "***\n"; }

Good luck in your adventures!


In reply to Re: question about $_ and eof by wmono
in thread question about $_ and eof by wageslave

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.