Aloha Monks! So, I continue on my journey to understand Perl and I still get tripped up by the simplicity of it as I guess I am trying to do this the hard way. I picked up a Perl book and am working through the excercises. I eventually worked this out in a section on loops:
use strict; my $target = 12; print "Guess my number.\n"; #my $guess = <STDIN>; # Attempt 2 with the "next" statements below co +mmented out. This failed also. print "Enter your guess: "; #my $guess = <STDIN>; Attempt 1 worked but had to enter the guess a s +econd time on a new line before processing my $guess; while ( $guess = <STDIN>) { if ($guess == $target) { print "You guessed it!"; last; }elsif ($guess > $target) { print "Your guess is too high.\n"; #next; # Attempt 1 using this failed. }elsif ($guess < $target) { print "Your guess is too low.\n"; #next; # Attempt 1 using this failed. } print "Enter your guess: "; }
I'm puzzled as to why not declaring my $guess; as opposed to using  my $code = <STDIN>; didn't really work. Is it because by using this last statement confilicts with the  while ($guess = <STDIN>) statement? I've kept some comments in as they show my different attempts to make this work. Thank you for any advice you can offer on my problem. I'm sure there are leaner ways to design this program, but I am just trying to understand the variables here rather than make it lean and mean.

In reply to Regarding Declaring Variables by Tech77

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.