Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The book Im learning perl from has very little information on the special variable $_ all it basically says is that if <STDIN> isnt assigned to a variable its assigned to $_ and it can be used with a foreach loop I've search some on the web but most of the tutorials I've run across have relativley little information on $_, although i've learned a bit more on it. one of the things I found out was that it works with a while loop, so I tried a few different ways of using it. but i cant figure out why it doesnt work for the second one and why the third one seems to be off by one every time it prints
print "first\n"; while (<STDIN>) { print; print "***\n"; last if(eof); } print "second\n"; while (<STDIN>!=eof) { print; print "***\n"; } print "\nthird\n"; while ((chomp($tst=<STDIN>))!=eof) {print $tst ; print "***\n"; }
I'm runing this on a windoze system. the first while statement works as I expect it to,basically you type something hit return it prints what you typed when you hit ctrl-z it exits.
the second one doesnt work, if you hit enter twice it exits out of the loop, from what I can guess is that $_ isn't being assigned, but no idea why it wont do the comparison .
The third one works buts its a lil off, after the second entry it prints stuff. from what I understand all 3 should produce the same output if someone could help me out and set me straight as to why the outputs are different I would greatly appreciate it

In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-12-02 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (16 votes). Check out past polls.

    Notices?